<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <meta name="generator" content="VuePress 2.0.0-beta.66">
    <style>
      :root {
        --c-bg: #fff;
      }
      html.dark {
        --c-bg: #22272e;
      }
      html, body {
        background-color: var(--c-bg);
      }
    </style>
    <script>
      const userMode = localStorage.getItem('vuepress-color-scheme');
			const systemDarkMode = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
			if (userMode === 'dark' || (userMode !== 'light' && systemDarkMode)) {
				document.documentElement.classList.toggle('dark', true);
			}
    </script>
    <title>Numpy | 你好， VuePress ！</title><meta name="description" content="这是我的第一个 VuePress 站点">
    <link rel="preload" href="/assets/style-8a38cc2d.css" as="style"><link rel="stylesheet" href="/assets/style-8a38cc2d.css">
    <link rel="modulepreload" href="/assets/app-6c74e544.js"><link rel="modulepreload" href="/assets/Numpy.html-742cd1c8.js"><link rel="modulepreload" href="/assets/Numpy.html-6ea20a5f.js"><link rel="prefetch" href="/assets/index.html-75ca5b8c.js" as="script"><link rel="prefetch" href="/assets/化学.html-bcd92d03.js" as="script"><link rel="prefetch" href="/assets/IDEA.html-ce9ad873.js" as="script"><link rel="prefetch" href="/assets/python调用idm下载.html-615c23bf.js" as="script"><link rel="prefetch" href="/assets/index.html-571c49cc.js" as="script"><link rel="prefetch" href="/assets/VuePress2x搭建.html-910a2c4e.js" as="script"><link rel="prefetch" href="/assets/WindowsTerminal.html-4a75819f.js" as="script"><link rel="prefetch" href="/assets/乱七八糟.html-0ad5c8cc.js" as="script"><link rel="prefetch" href="/assets/国内访问GitHub.html-bd9bc6eb.js" as="script"><link rel="prefetch" href="/assets/虚拟机安装凤凰OS.html-fd472c84.js" as="script"><link rel="prefetch" href="/assets/软件工程过程.html-ad186a25.js" as="script"><link rel="prefetch" href="/assets/Java.html-a9e8e4b2.js" as="script"><link rel="prefetch" href="/assets/latex.html-b66b07c2.js" as="script"><link rel="prefetch" href="/assets/Markdown.html-76abef01.js" as="script"><link rel="prefetch" href="/assets/Markdown_vuepress.html-7a108572.js" as="script"><link rel="prefetch" href="/assets/python.html-80001bc2.js" as="script"><link rel="prefetch" href="/assets/python爬虫.html-0eaf5c34.js" as="script"><link rel="prefetch" href="/assets/index.html-404afaec.js" as="script"><link rel="prefetch" href="/assets/Redis.html-3c0347e6.js" as="script"><link rel="prefetch" href="/assets/Selenium.html-dd74eb14.js" as="script"><link rel="prefetch" href="/assets/快速幂算法 牛客小白月赛 1-C分元宵.html-6a00d92c.js" as="script"><link rel="prefetch" href="/assets/数据库.html-a9b825b9.js" as="script"><link rel="prefetch" href="/assets/数据结构_学校.html-db54f988.js" as="script"><link rel="prefetch" href="/assets/404.html-f9875e7b.js" as="script"><link rel="prefetch" href="/assets/index.html-f9be7961.js" as="script"><link rel="prefetch" href="/assets/化学.html-55546e87.js" as="script"><link rel="prefetch" href="/assets/IDEA.html-3d6ee1dc.js" as="script"><link rel="prefetch" href="/assets/python调用idm下载.html-862a9ae4.js" as="script"><link rel="prefetch" href="/assets/index.html-71e4b30e.js" as="script"><link rel="prefetch" href="/assets/VuePress2x搭建.html-69ed53cb.js" as="script"><link rel="prefetch" href="/assets/WindowsTerminal.html-d051c0b9.js" as="script"><link rel="prefetch" href="/assets/乱七八糟.html-e6e19f4d.js" as="script"><link rel="prefetch" href="/assets/国内访问GitHub.html-5a1e283c.js" as="script"><link rel="prefetch" href="/assets/虚拟机安装凤凰OS.html-3f19c5ba.js" as="script"><link rel="prefetch" href="/assets/软件工程过程.html-d5033030.js" as="script"><link rel="prefetch" href="/assets/Java.html-07b672b0.js" as="script"><link rel="prefetch" href="/assets/latex.html-139cf75a.js" as="script"><link rel="prefetch" href="/assets/Markdown.html-24c02676.js" as="script"><link rel="prefetch" href="/assets/Markdown_vuepress.html-a9f40501.js" as="script"><link rel="prefetch" href="/assets/python.html-da27b5e1.js" as="script"><link rel="prefetch" href="/assets/python爬虫.html-22e08aed.js" as="script"><link rel="prefetch" href="/assets/index.html-25c668e6.js" as="script"><link rel="prefetch" href="/assets/Redis.html-732c58b4.js" as="script"><link rel="prefetch" href="/assets/Selenium.html-8840df78.js" as="script"><link rel="prefetch" href="/assets/快速幂算法 牛客小白月赛 1-C分元宵.html-41e7e00f.js" as="script"><link rel="prefetch" href="/assets/数据库.html-e55ff260.js" as="script"><link rel="prefetch" href="/assets/数据结构_学校.html-cf35a9b9.js" as="script"><link rel="prefetch" href="/assets/404.html-ad53950d.js" as="script"><link rel="prefetch" href="/assets/mermaid-9e549946.js" as="script">
  </head>
  <body>
    <div id="app"><!--[--><div class="theme-container"><!--[--><header class="navbar"><div class="toggle-sidebar-button" title="toggle sidebar" aria-expanded="false" role="button" tabindex="0"><div class="icon" aria-hidden="true"><span></span><span></span><span></span></div></div><span><a href="/" class=""><img class="logo" src="/img/hero.png" alt="你好， VuePress ！"><span class="site-name can-hide">你好， VuePress ！</span></a></span><div class="navbar-items-wrapper" style=""><!--[--><!--]--><nav class="navbar-items can-hide"><!--[--><div class="navbar-item"><div class="navbar-dropdown-wrapper"><button class="navbar-dropdown-title" type="button" aria-label="编程"><span class="title">编程</span><span class="arrow down"></span></button><button class="navbar-dropdown-title-mobile" type="button" aria-label="编程"><span class="title">编程</span><span class="right arrow"></span></button><ul style="display:none;" class="navbar-dropdown"><!--[--><li class="navbar-dropdown-item"><a href="/program/Java.md" class="" aria-label="Java"><!--[--><!--]--> Java <!--[--><!--]--></a></li><li class="navbar-dropdown-item"><a href="/program/Python.md" class="" aria-label="Python"><!--[--><!--]--> Python <!--[--><!--]--></a></li><li class="navbar-dropdown-item"><!--[--><h4 class="navbar-dropdown-subtitle"><span>笔记</span></h4><ul class="navbar-dropdown-subitem-wrapper"><!--[--><li class="navbar-dropdown-subitem"><a href="/program/Markdown.html" class="" aria-label="Markdown"><!--[--><!--]--> Markdown <!--[--><!--]--></a></li><li class="navbar-dropdown-subitem"><a href="/program/latex.html" class="" aria-label="Latex"><!--[--><!--]--> Latex <!--[--><!--]--></a></li><!--]--></ul><!--]--></li><li class="navbar-dropdown-item"><a href="/program/数据库.md" class="" aria-label="数据库"><!--[--><!--]--> 数据库 <!--[--><!--]--></a></li><!--]--></ul></div></div><div class="navbar-item"><a href="/hx/化学.md" class="" aria-label="化学"><!--[--><!--]--> 化学 <!--[--><!--]--></a></div><div class="navbar-item"><a href="/others/" class="" aria-label="乱七八糟"><!--[--><!--]--> 乱七八糟 <!--[--><!--]--></a></div><div class="navbar-item"><a class="external-link" href="https://gitee.com/Francis-xsc/francis-xsc" rel="noopener noreferrer" target="_blank" aria-label="Gitee"><!--[--><!--]--> Gitee <span><svg class="external-link-icon" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg><span class="external-link-icon-sr-only">open in new window</span></span><!--[--><!--]--></a></div><!--]--></nav><!--[--><!--]--><button class="toggle-color-mode-button" title="toggle color mode"><svg style="" class="icon" focusable="false" viewBox="0 0 32 32"><path d="M16 12.005a4 4 0 1 1-4 4a4.005 4.005 0 0 1 4-4m0-2a6 6 0 1 0 6 6a6 6 0 0 0-6-6z" fill="currentColor"></path><path d="M5.394 6.813l1.414-1.415l3.506 3.506L8.9 10.318z" fill="currentColor"></path><path d="M2 15.005h5v2H2z" fill="currentColor"></path><path d="M5.394 25.197L8.9 21.691l1.414 1.415l-3.506 3.505z" fill="currentColor"></path><path d="M15 25.005h2v5h-2z" fill="currentColor"></path><path d="M21.687 23.106l1.414-1.415l3.506 3.506l-1.414 1.414z" fill="currentColor"></path><path d="M25 15.005h5v2h-5z" fill="currentColor"></path><path d="M21.687 8.904l3.506-3.506l1.414 1.415l-3.506 3.505z" fill="currentColor"></path><path d="M15 2.005h2v5h-2z" fill="currentColor"></path></svg><svg style="display:none;" class="icon" focusable="false" viewBox="0 0 32 32"><path d="M13.502 5.414a15.075 15.075 0 0 0 11.594 18.194a11.113 11.113 0 0 1-7.975 3.39c-.138 0-.278.005-.418 0a11.094 11.094 0 0 1-3.2-21.584M14.98 3a1.002 1.002 0 0 0-.175.016a13.096 13.096 0 0 0 1.825 25.981c.164.006.328 0 .49 0a13.072 13.072 0 0 0 10.703-5.555a1.01 1.01 0 0 0-.783-1.565A13.08 13.08 0 0 1 15.89 4.38A1.015 1.015 0 0 0 14.98 3z" fill="currentColor"></path></svg></button><!----></div></header><!--]--><div class="sidebar-mask"></div><!--[--><aside class="sidebar"><nav class="navbar-items"><!--[--><div class="navbar-item"><div class="navbar-dropdown-wrapper"><button class="navbar-dropdown-title" type="button" aria-label="编程"><span class="title">编程</span><span class="arrow down"></span></button><button class="navbar-dropdown-title-mobile" type="button" aria-label="编程"><span class="title">编程</span><span class="right arrow"></span></button><ul style="display:none;" class="navbar-dropdown"><!--[--><li class="navbar-dropdown-item"><a href="/program/Java.md" class="" aria-label="Java"><!--[--><!--]--> Java <!--[--><!--]--></a></li><li class="navbar-dropdown-item"><a href="/program/Python.md" class="" aria-label="Python"><!--[--><!--]--> Python <!--[--><!--]--></a></li><li class="navbar-dropdown-item"><!--[--><h4 class="navbar-dropdown-subtitle"><span>笔记</span></h4><ul class="navbar-dropdown-subitem-wrapper"><!--[--><li class="navbar-dropdown-subitem"><a href="/program/Markdown.html" class="" aria-label="Markdown"><!--[--><!--]--> Markdown <!--[--><!--]--></a></li><li class="navbar-dropdown-subitem"><a href="/program/latex.html" class="" aria-label="Latex"><!--[--><!--]--> Latex <!--[--><!--]--></a></li><!--]--></ul><!--]--></li><li class="navbar-dropdown-item"><a href="/program/数据库.md" class="" aria-label="数据库"><!--[--><!--]--> 数据库 <!--[--><!--]--></a></li><!--]--></ul></div></div><div class="navbar-item"><a href="/hx/化学.md" class="" aria-label="化学"><!--[--><!--]--> 化学 <!--[--><!--]--></a></div><div class="navbar-item"><a href="/others/" class="" aria-label="乱七八糟"><!--[--><!--]--> 乱七八糟 <!--[--><!--]--></a></div><div class="navbar-item"><a class="external-link" href="https://gitee.com/Francis-xsc/francis-xsc" rel="noopener noreferrer" target="_blank" aria-label="Gitee"><!--[--><!--]--> Gitee <span><svg class="external-link-icon" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg><span class="external-link-icon-sr-only">open in new window</span></span><!--[--><!--]--></a></div><!--]--></nav><!--[--><!--]--><ul class="sidebar-items"><!--[--><li><a href="/program/Java.md" class="sidebar-item sidebar-heading collapsible" aria-label="Java"><!--[--><!--]--> Java <!--[--><!--]--></a><!----></li><li><p tabindex="0" class="sidebar-item sidebar-heading collapsible">Python <span class="right arrow"></span></p><ul style="display:none;" class="sidebar-item-children"><!--[--><li><a href="/program/python.html" class="sidebar-item" aria-label="python-北理工"><!--[--><!--]--> python-北理工 <!--[--><!--]--></a><!----></li><!--]--></ul></li><li><p tabindex="0" class="sidebar-item sidebar-heading collapsible">Markdown <span class="right arrow"></span></p><ul style="display:none;" class="sidebar-item-children"><!--[--><li><a href="/program/Markdown.html" class="sidebar-item" aria-label="Markdown"><!--[--><!--]--> Markdown <!--[--><!--]--></a><!----></li><!--]--></ul></li><!--]--></ul><!--[--><!--]--></aside><!--]--><!--[--><main class="page"><!--[--><!--]--><div class="theme-default-content"><!--[--><!--]--><div><h1 id="numpy" tabindex="-1"><a class="header-anchor" href="#numpy" aria-hidden="true">#</a> Numpy</h1><p>参考：</p><p><a href="http://c.biancheng.net/numpy/" target="_blank" rel="noopener noreferrer">NumPy教程（C语言中文网）<span><svg class="external-link-icon" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg><span class="external-link-icon-sr-only">open in new window</span></span></a></p><h2 id="array" tabindex="-1"><a class="header-anchor" href="#array" aria-hidden="true">#</a> array()</h2><p><code>numpy.array(object, dtype = None, copy = True, order = None,ndmin = 0)</code></p><table><thead><tr><th>序号</th><th>参数</th><th>描述说明</th></tr></thead><tbody><tr><td>1</td><td>object</td><td>表示一个数组序列。</td></tr><tr><td>2</td><td>dtype</td><td>可选参数，通过它可以更改数组的数据类型。</td></tr><tr><td>3</td><td>copy</td><td>可选参数，表示数组能否被复制，默认是 True。</td></tr><tr><td>4</td><td>order</td><td>以哪种内存布局创建数组，有 3 个可选值，分别是 C(行序列)/F(列序列)/A(默认)。</td></tr><tr><td>5</td><td>ndim</td><td>用于指定数组的维度。</td></tr></tbody></table><h2 id="ndim查看数组维数" tabindex="-1"><a class="header-anchor" href="#ndim查看数组维数" aria-hidden="true">#</a> ndim查看数组维数</h2><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np 
arr <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">11</span><span class="token punctuation">,</span> <span class="token number">23</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span> 
<span class="token keyword">print</span><span class="token punctuation">(</span>arr<span class="token punctuation">.</span>ndim<span class="token punctuation">)</span> 
<span class="token number">2</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code>a <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">,</span> ndim <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div></div></div><h2 id="reshape数组变维" tabindex="-1"><a class="header-anchor" href="#reshape数组变维" aria-hidden="true">#</a> reshape数组变维</h2><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np 
e <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span> 
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;原数组&quot;</span><span class="token punctuation">,</span>e<span class="token punctuation">)</span> 
e<span class="token operator">=</span>e<span class="token punctuation">.</span>reshape<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">)</span> 
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;新数组&quot;</span><span class="token punctuation">,</span>e<span class="token punctuation">)</span>  
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="数据类型" tabindex="-1"><a class="header-anchor" href="#数据类型" aria-hidden="true">#</a> 数据类型</h2><table><thead><tr><th>序号</th><th>数据类型</th><th>语言描述</th></tr></thead><tbody><tr><td>1</td><td>bool_</td><td>布尔型数据类型（True 或者 False）</td></tr><tr><td>2</td><td>int_</td><td>默认整数类型，类似于 C 语言中的 long，取值为 int32 或 int64</td></tr><tr><td>3</td><td>intc</td><td>和 C 语言的 int 类型一样，一般是 int32 或 int 64</td></tr><tr><td>4</td><td>intp</td><td>用于索引的整数类型（类似于 C 的 ssize_t，通常为 int32 或 int64）</td></tr><tr><td>5</td><td>int8</td><td>代表与1字节相同的8位整数。值的范围是-128到127。</td></tr><tr><td>6</td><td>int16</td><td>代表 2 字节（16位）的整数。范围是-32768至32767。</td></tr><tr><td>7</td><td>int32</td><td>代表 4 字节（32位）整数。范围是-2147483648至2147483647。</td></tr><tr><td>8</td><td>int64</td><td>表示 8 字节（64位）整数。范围是-9223372036854775808至9223372036854775807。</td></tr><tr><td>9</td><td>uint8</td><td>代表1字节（8位）无符号整数。</td></tr><tr><td>10</td><td>uint16</td><td>2 字节（16位）无符号整数。</td></tr><tr><td>11</td><td>uint32</td><td>4 字节（32位）的无符号整数。</td></tr><tr><td>12</td><td>uint64</td><td>8 字节（64位）的无符号整数。</td></tr><tr><td>13</td><td>float_</td><td>float64 类型的简写。</td></tr><tr><td>14</td><td>float16</td><td>半精度浮点数，包括：1 个符号位，5 个指数位，10个尾数位。</td></tr><tr><td>15</td><td>float32</td><td>单精度浮点数，包括：1 个符号位，8 个指数位，23个尾数位。</td></tr><tr><td>16</td><td>float64</td><td>双精度浮点数，包括：1 个符号位，11 个指数位，52个尾数位。</td></tr><tr><td>17</td><td>complex_</td><td>复数类型，与 complex128 类型相同。</td></tr><tr><td>18</td><td>complex64</td><td>表示实部和虚部共享 32 位的复数。</td></tr><tr><td>19</td><td>complex128</td><td>表示实部和虚部共享 64 位的复数。</td></tr><tr><td>20</td><td>str_</td><td>表示字符串类型</td></tr><tr><td>21</td><td>string_</td><td>表示字节串类型</td></tr></tbody></table><h2 id="数据类型对象" tabindex="-1"><a class="header-anchor" href="#数据类型对象" aria-hidden="true">#</a> 数据类型对象</h2><p><code>np.dtype(object)</code></p><h2 id="数据类型标识码" tabindex="-1"><a class="header-anchor" href="#数据类型标识码" aria-hidden="true">#</a> 数据类型标识码</h2><table><thead><tr><th>字符</th><th>对应类型</th></tr></thead><tbody><tr><td>b</td><td>代表布尔型</td></tr><tr><td>i</td><td>带符号整型</td></tr><tr><td>u</td><td>无符号整型</td></tr><tr><td>f</td><td>浮点型</td></tr><tr><td>c</td><td>复数浮点型</td></tr><tr><td>m</td><td>时间间隔（timedelta）</td></tr><tr><td>M</td><td>datatime（日期时间）</td></tr><tr><td>O</td><td>Python对象</td></tr><tr><td>S,a</td><td>字节串（S）与字符串（a）</td></tr><tr><td>U</td><td>Unicode</td></tr><tr><td>V</td><td>原始数据（void）</td></tr></tbody></table><h2 id="定义结构化数据" tabindex="-1"><a class="header-anchor" href="#定义结构化数据" aria-hidden="true">#</a> 定义结构化数据</h2><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
teacher <span class="token operator">=</span> np<span class="token punctuation">.</span>dtype<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">(</span><span class="token string">&#39;name&#39;</span><span class="token punctuation">,</span><span class="token string">&#39;S20&#39;</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token string">&#39;age&#39;</span><span class="token punctuation">,</span> <span class="token string">&#39;i1&#39;</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token string">&#39;salary&#39;</span><span class="token punctuation">,</span> <span class="token string">&#39;f4&#39;</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token comment">#输出结构化数据teacher</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>teacher<span class="token punctuation">)</span>
<span class="token comment">#将其应用于ndarray对象</span>
b <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">(</span><span class="token string">&#39;ycs&#39;</span><span class="token punctuation">,</span> <span class="token number">32</span><span class="token punctuation">,</span> <span class="token number">6357.50</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token string">&#39;jxe&#39;</span><span class="token punctuation">,</span> <span class="token number">28</span><span class="token punctuation">,</span> <span class="token number">6856.80</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">,</span> dtype <span class="token operator">=</span> teacher<span class="token punctuation">)</span> 
<span class="token keyword">print</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><div class="language-text line-numbers-mode" data-ext="text"><pre class="language-text"><code>[(&#39;name&#39;, &#39;S20&#39;), (&#39;age&#39;, &#39;i1&#39;), (&#39;salary&#39;, &#39;&lt;f4&#39;)]
#输出的name为bytes字节串类型
[(b&#39;ycs&#39;, 32, 6357.5) (b&#39;jxe&#39;, 28, 6856.8)]
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="ndarray-shape" tabindex="-1"><a class="header-anchor" href="#ndarray-shape" aria-hidden="true">#</a> ndarray.shape</h2><p>可通过shape改变数组形状</p><h2 id="ndarray-reshape" tabindex="-1"><a class="header-anchor" href="#ndarray-reshape" aria-hidden="true">#</a> ndarray.reshape()</h2><h2 id="ndarray-ndim" tabindex="-1"><a class="header-anchor" href="#ndarray-ndim" aria-hidden="true">#</a> ndarray.ndim</h2><p>数组的维数</p><h2 id="ndarray-itemsize" tabindex="-1"><a class="header-anchor" href="#ndarray-itemsize" aria-hidden="true">#</a> ndarray.itemsize</h2><p>数组中每个元素的大小（以字节为单位）</p><h2 id="ndarray-flags" tabindex="-1"><a class="header-anchor" href="#ndarray-flags" aria-hidden="true">#</a> ndarray.flags</h2><div class="language-text line-numbers-mode" data-ext="text"><pre class="language-text"><code>C_CONTIGUOUS : True
F_CONTIGUOUS : True
OWNDATA : True
WRITEABLE : True
ALIGNED : True
WRITEBACKIFCOPY : False
UPDATEIFCOPY : False
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="numpy-empty" tabindex="-1"><a class="header-anchor" href="#numpy-empty" aria-hidden="true">#</a> numpy.empty()</h2><p><code>numpy.empty(shape, dtype = float, order = &#39;C&#39;)</code></p><ul><li>shape：指定数组的形状；</li><li>dtype：数组元素的数据类型，默认值是值 float；</li><li>order：指数组元素在计算机内存中的储存顺序，默认顺序是“C”(行优先顺序)。</li></ul><h2 id="numpy-zeros" tabindex="-1"><a class="header-anchor" href="#numpy-zeros" aria-hidden="true">#</a> numpy.zeros()</h2><p><code>numpy. zeros(shape,dtype=float,order=&quot;C&quot;)</code></p><table><thead><tr><th>参数名称</th><th>说明描述</th></tr></thead><tbody><tr><td>shape</td><td>指定数组的形状大小。</td></tr><tr><td>dtype</td><td>可选项，数组的数据类型</td></tr><tr><td>order</td><td>“C”代表以行顺序存储，“F”则表示以列顺序存储</td></tr></tbody></table><h2 id="numpy-ones" tabindex="-1"><a class="header-anchor" href="#numpy-ones" aria-hidden="true">#</a> numpy.ones()</h2><p><code>numpy.ones(shape, dtype = None, order = &#39;C&#39;)</code></p><h2 id="numpy-asarray" tabindex="-1"><a class="header-anchor" href="#numpy-asarray" aria-hidden="true">#</a> numpy.asarray()</h2><p><code>numpy.asarray（sequence，dtype = None ，order = None ）</code></p><ul><li>sequence：接受一个 Python 序列，可以是列表或者元组；</li><li>dtype：可选参数，数组的数据类型；</li><li>order：数组内存布局样式，可以设置为 C 或者 F，默认是 C。</li></ul><h2 id="numpy-frombuffer" tabindex="-1"><a class="header-anchor" href="#numpy-frombuffer" aria-hidden="true">#</a> numpy.frombuffer()</h2><p><code>numpy.frombuffer(buffer, dtype = float, count = -1, offset = 0)</code></p><ul><li>buffer：将任意对象转换为流的形式读入缓冲区；</li><li>dtype：返回数组的数据类型，默认是 float32；</li><li>count：要读取的数据数量，默认为 -1 表示读取所有数据；</li><li>offset：读取数据的起始位置，默认为 0。</li></ul><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np 
<span class="token comment">#字节串类型</span>
l <span class="token operator">=</span> <span class="token string">b&#39;hello world&#39;</span> 
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token builtin">type</span><span class="token punctuation">(</span>l<span class="token punctuation">)</span><span class="token punctuation">)</span> 
a <span class="token operator">=</span> np<span class="token punctuation">.</span>frombuffer<span class="token punctuation">(</span>l<span class="token punctuation">,</span> dtype <span class="token operator">=</span> <span class="token string">&quot;S1&quot;</span><span class="token punctuation">)</span> 
<span class="token keyword">print</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span> 
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token builtin">type</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">)</span> 
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><div class="language-text line-numbers-mode" data-ext="text"><pre class="language-text"><code>&lt;class &#39;bytes&#39;&gt;
[b&#39;h&#39; b&#39;e&#39; b&#39;l&#39; b&#39;l&#39; b&#39;o&#39; b&#39; &#39; b&#39;w&#39; b&#39;o&#39; b&#39;r&#39; b&#39;l&#39; b&#39;d&#39;]
&lt;class &#39;numpy.ndarray&#39;&gt;
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="numpy-fromiter" tabindex="-1"><a class="header-anchor" href="#numpy-fromiter" aria-hidden="true">#</a> numpy.fromiter()</h2><p><code>numpy.fromiter(iterable, dtype, count = -1)</code></p><table><thead><tr><th>参数名称</th><th>描述说明</th></tr></thead><tbody><tr><td>iterable</td><td>可迭代对象。</td></tr><tr><td>dtype</td><td>返回数组的数据类型。</td></tr><tr><td>count</td><td>读取的数据数量，默认为 -1，读取所有数据。</td></tr></tbody></table><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
<span class="token comment"># 使用 range 函数创建列表对象 </span>
<span class="token builtin">list</span><span class="token operator">=</span><span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">6</span><span class="token punctuation">)</span>
<span class="token comment">#生成可迭代对象i</span>
i<span class="token operator">=</span><span class="token builtin">iter</span><span class="token punctuation">(</span><span class="token builtin">list</span><span class="token punctuation">)</span>
<span class="token comment">#使用i迭代器，通过fromiter方法创建ndarray</span>
array<span class="token operator">=</span>np<span class="token punctuation">.</span>fromiter<span class="token punctuation">(</span>i<span class="token punctuation">,</span> dtype<span class="token operator">=</span><span class="token builtin">float</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>array<span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="numpy-arange" tabindex="-1"><a class="header-anchor" href="#numpy-arange" aria-hidden="true">#</a> numpy.arange()</h2><p><code>numpy.arange(start, stop, step, dtype)</code></p><table><thead><tr><th>参数名称</th><th>参数说明</th></tr></thead><tbody><tr><td>start</td><td>起始值，默认是 0。</td></tr><tr><td>stop</td><td>终止值，注意生成的数组元素值不包含终止值。</td></tr><tr><td>step</td><td>步长，默认为 1。</td></tr><tr><td>dtype</td><td>可选参数，指定 ndarray 数组的数据类型。</td></tr></tbody></table><h2 id="numpy-linspace" tabindex="-1"><a class="header-anchor" href="#numpy-linspace" aria-hidden="true">#</a> numpy.linspace()</h2><p>返回均匀间隔的一维等差数组，默认均分 50 份</p><p><code>np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)</code></p><ul><li>start：代表数值区间的起始值；</li><li>stop：代表数值区间的终止值；</li><li>num：表示数值区间内要生成多少个均匀的样本。默认值为 50；</li><li>endpoint：默认为 True，表示数列包含 stop 终止值，反之不包含；</li><li>retstep：默认为 True，表示生成的数组中会显示公差项，反之不显示；</li><li>dtype：代表数组元素值的数据类型。</li></ul><h2 id="numpy-logspace" tabindex="-1"><a class="header-anchor" href="#numpy-logspace" aria-hidden="true">#</a> numpy.logspace</h2><p>用于创建等比数组</p><p><code>np.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None)</code></p><table><thead><tr><th>参数名称</th><th>说明描述</th></tr></thead><tbody><tr><td>start</td><td>序列的起始值：base**start。</td></tr><tr><td>stop</td><td>序列的终止值：base**stop。</td></tr><tr><td>num</td><td>数值范围区间内样本数量，默认为 50。</td></tr><tr><td>endpoint</td><td>默认为 True 包含终止值，反之不包含。</td></tr><tr><td>base</td><td>对数函数的 log 底数，默认为10。</td></tr><tr><td>dtype</td><td>可选参数，指定 ndarray 数组的数据类型。</td></tr></tbody></table><h2 id="切片" tabindex="-1"><a class="header-anchor" href="#切片" aria-hidden="true">#</a> 切片</h2><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
a <span class="token operator">=</span> np<span class="token punctuation">.</span>arange<span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span>
<span class="token comment">#生成切片对象</span>
s <span class="token operator">=</span> <span class="token builtin">slice</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">9</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token comment">#从索引2开始到索引9停止，间隔时间为2</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>a<span class="token punctuation">[</span>s<span class="token punctuation">]</span><span class="token punctuation">)</span>

b <span class="token operator">=</span> a<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">:</span><span class="token number">9</span><span class="token punctuation">:</span><span class="token number">2</span><span class="token punctuation">]</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
<span class="token comment">#创建a数组</span>
a <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token comment">#返回数组的第二列</span>
<span class="token keyword">print</span> <span class="token punctuation">(</span>a<span class="token punctuation">[</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span> 
<span class="token comment">#返回数组的第二行</span>
<span class="token keyword">print</span> <span class="token punctuation">(</span>a<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token comment">#返回第二列后的所有项</span>
<span class="token keyword">print</span> <span class="token punctuation">(</span>a<span class="token punctuation">[</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
a<span class="token operator">=</span>np<span class="token punctuation">.</span>arange<span class="token punctuation">(</span><span class="token number">16</span><span class="token punctuation">)</span><span class="token punctuation">.</span>reshape<span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>a<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span>
<span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token number">0</span>  <span class="token number">1</span>  <span class="token number">2</span>  <span class="token number">3</span><span class="token punctuation">]</span>
 <span class="token punctuation">[</span> <span class="token number">4</span>  <span class="token number">5</span>  <span class="token number">6</span>  <span class="token number">7</span><span class="token punctuation">]</span>
 <span class="token punctuation">[</span> <span class="token number">8</span>  <span class="token number">9</span> <span class="token number">10</span> <span class="token number">11</span><span class="token punctuation">]</span>
 <span class="token punctuation">[</span><span class="token number">12</span> <span class="token number">13</span> <span class="token number">14</span> <span class="token number">15</span><span class="token punctuation">]</span><span class="token punctuation">]</span>
<span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token number">5</span>  <span class="token number">6</span><span class="token punctuation">]</span>
 <span class="token punctuation">[</span> <span class="token number">9</span> <span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">]</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>x[[0,1,2],[0,1,0]]</p><div class="language-text line-numbers-mode" data-ext="text"><pre class="language-text"><code>import numpy as np
a=np.arange(20).reshape(4,5)
print(a)
row=[[0,0],[3,3]]
column=[[0,4],[0,4]]
print(a[row,column])
row=[0,0,3,3]
column=[0,4,0,4]
print(a[row,column])
--------------
[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]
 [15 16 17 18 19]]
[[ 0  4]
 [15 19]]
[ 0  4 15 19]
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><div class="language-text line-numbers-mode" data-ext="text"><pre class="language-text"><code>import numpy as np
d = np.array([[ 0,  1,  2],
              [ 3,  4,  5],
              [ 6,  7,  8],
              [ 9, 10, 11]])
#对行列分别进行切片
e = d[1:4,1:3]
print(e)
#行使用基础索引，对列使用高级索引
f = d[1:4,[1,2]]
#显示切片后结果
print (f)
#对行使用省略号
h=d[...,1:]
print(h)
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="布尔数组索引" tabindex="-1"><a class="header-anchor" href="#布尔数组索引" aria-hidden="true">#</a> 布尔数组索引</h2><div class="language-text line-numbers-mode" data-ext="text"><pre class="language-text"><code>#返回所有大于6的数字组成的数组
import numpy as np
x = np.array([[ 0,  1,  2],[ 3,  4,  5],[ 6,  7,  8],[ 9, 10, 11]])
print (x[x &gt; 6])
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>可以使用补码运算符来去除 NaN（即非数字元素）</p><div class="language-text line-numbers-mode" data-ext="text"><pre class="language-text"><code>import numpy as np
a = np.array([np.nan, 1,2,np.nan,3,4,5])
print(a[~np.isnan(a)])
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>删除数组中整数元素</p><div class="language-text line-numbers-mode" data-ext="text"><pre class="language-text"><code>import numpy as np
a = np.array([1, 2+6j, 5, 3.5+5j])
print( a[np.iscomplex(a)])
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>同时使用多个索引数组，需要添加<code>np.ix_</code></p><div class="language-text line-numbers-mode" data-ext="text"><pre class="language-text"><code>import numpy as np
x=np.arange(32).reshape((8,4))
print (x[np.ix_([1,5,7,2],[0,3,1,2])])
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="numpy遍历数组" tabindex="-1"><a class="header-anchor" href="#numpy遍历数组" aria-hidden="true">#</a> NumPy遍历数组</h2><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token keyword">for</span> x <span class="token keyword">in</span> np<span class="token punctuation">.</span>nditer<span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">:</span>
   <span class="token keyword">print</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span>

np<span class="token punctuation">.</span>nditer<span class="token punctuation">(</span>a<span class="token punctuation">,</span> order <span class="token operator">=</span> <span class="token string">&#39;F&#39;</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>a与a.T在内存中的存储顺序一致</p><p>nditer 对象提供了一个可选参数<code>op_flags</code>，它表示能否在遍历数组时对元素进行修改。它提供了三种模式，如下所示：</p><ul><li>read-only <ul><li>只读模式，在这种模式下，遍历时不能修改数组中的元素。</li></ul></li><li>read-write <ul><li>读写模式，遍历时可以修改元素值。</li></ul></li><li>write-only <ul><li>只写模式，在遍历时可以修改元素值。</li></ul></li></ul><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
a <span class="token operator">=</span> np<span class="token punctuation">.</span>arange<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">60</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">)</span>
a <span class="token operator">=</span> a<span class="token punctuation">.</span>reshape<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">)</span> 
<span class="token keyword">print</span> <span class="token punctuation">(</span><span class="token string">&quot;原数组是:&quot;</span><span class="token punctuation">,</span>a<span class="token punctuation">)</span>
<span class="token keyword">for</span> x <span class="token keyword">in</span> np<span class="token punctuation">.</span>nditer<span class="token punctuation">(</span>a<span class="token punctuation">,</span> op_flags<span class="token operator">=</span><span class="token punctuation">[</span><span class="token string">&#39;readwrite&#39;</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    x<span class="token punctuation">[</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">2</span><span class="token operator">*</span>x
<span class="token keyword">print</span> <span class="token punctuation">(</span><span class="token string">&#39;修改后的数组是：&#39;</span><span class="token punctuation">,</span>a<span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="外部循环使用" tabindex="-1"><a class="header-anchor" href="#外部循环使用" aria-hidden="true">#</a> 外部循环使用</h3><p>flags参数</p><table><thead><tr><th>参数值</th><th>描述说明</th></tr></thead><tbody><tr><td>c_index</td><td>可以跟踪 C 顺序的索引。</td></tr><tr><td>f_index</td><td>可以跟踪 Fortran 顺序的索引。</td></tr><tr><td>multi_index</td><td>每次迭代都会跟踪一种索引类型。</td></tr><tr><td>external_loop</td><td>返回的遍历结果是具有多个值的一维数组。</td></tr></tbody></table><h2 id="数组变维操作" tabindex="-1"><a class="header-anchor" href="#数组变维操作" aria-hidden="true">#</a> 数组变维操作</h2><table><thead><tr><th>函数名称</th><th>函数介绍</th></tr></thead><tbody><tr><td>reshape</td><td>在不改变数组元素的条件下，修改数组的形状。</td></tr><tr><td>flat</td><td>返回是一个迭代器，可以用 for 循环遍历其中的每一个元素。</td></tr><tr><td>flatten</td><td>以一维数组的形式返回一份数组的副本，对副本的操作不会影响到原数组。</td></tr><tr><td>ravel</td><td>返回一个连续的扁平数组（即展开的一维数组），与 flatten不同，它返回的是数组视图（修改视图会影响原数组）。</td></tr></tbody></table><h3 id="numpy-ndarray-flat" tabindex="-1"><a class="header-anchor" href="#numpy-ndarray-flat" aria-hidden="true">#</a> numpy.ndarray.flat</h3><div class="language-text line-numbers-mode" data-ext="text"><pre class="language-text"><code>import numpy as np
a=np.arange(9).reshape(3,3)
for row in a:               #[0 1 2]
    print(row)              #[3 4 5]
                            #[6 7 8]
for ele in a.flat:
    print(ele,end=&#39; &#39;) 
# 0 1 2 3 4 5 6 7 8 
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="numpy-ndarray-flatten" tabindex="-1"><a class="header-anchor" href="#numpy-ndarray-flatten" aria-hidden="true">#</a> numpy.ndarray.flatten()</h3><p>numpy.ndarray.flatten 返回一份数组副本，对副本修改不会影响原始数组</p><p><code>ndarray.flatten(order=&#39;C&#39;)</code></p><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
a <span class="token operator">=</span> np<span class="token punctuation">.</span>arange<span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">.</span>reshape<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">)</span>
<span class="token keyword">print</span> <span class="token punctuation">(</span>a<span class="token punctuation">)</span>       <span class="token comment"># [[0 1 2 3]</span>
                <span class="token comment">#  [4 5 6 7]]</span>
<span class="token comment"># 默认按行C风格展开的数组</span>
<span class="token keyword">print</span> <span class="token punctuation">(</span>a<span class="token punctuation">.</span>flatten<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment"># [0 1 2 3 4 5 6 7]</span>
<span class="token comment"># 以F风格顺序展开的数组</span>
<span class="token keyword">print</span> <span class="token punctuation">(</span>a<span class="token punctuation">.</span>flatten<span class="token punctuation">(</span>order <span class="token operator">=</span> <span class="token string">&#39;F&#39;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment"># [0 4 1 5 2 6 3 7]</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="numpy-ravel" tabindex="-1"><a class="header-anchor" href="#numpy-ravel" aria-hidden="true">#</a> numpy.ravel()</h3><p>将多维数组中的元素以一维数组的形式展开，该方法返回数组的视图（view），如果修改，则会影响原始数组。</p><p><code>numpy.ravel(a, order=&#39;C&#39;)</code></p><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
a <span class="token operator">=</span> np<span class="token punctuation">.</span>arange<span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">.</span>reshape<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">)</span>
<span class="token keyword">print</span> <span class="token punctuation">(</span><span class="token string">&#39;原数组：&#39;</span><span class="token punctuation">)</span>
<span class="token keyword">print</span> <span class="token punctuation">(</span>a<span class="token punctuation">)</span>

<span class="token keyword">print</span> <span class="token punctuation">(</span><span class="token string">&#39;调用 ravel 函数后：&#39;</span><span class="token punctuation">)</span>
<span class="token keyword">print</span> <span class="token punctuation">(</span>a<span class="token punctuation">.</span>ravel<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment"># [0 1 2 3 4 5 6 7]</span>

<span class="token keyword">print</span> <span class="token punctuation">(</span><span class="token string">&#39;F 风格顺序调用 ravel 函数之后：&#39;</span><span class="token punctuation">)</span>
<span class="token keyword">print</span> <span class="token punctuation">(</span>a<span class="token punctuation">.</span>ravel<span class="token punctuation">(</span>order <span class="token operator">=</span> <span class="token string">&#39;F&#39;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment"># [0 4 1 5 2 6 3 7]</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="数组转置操作" tabindex="-1"><a class="header-anchor" href="#数组转置操作" aria-hidden="true">#</a> 数组转置操作</h2><table><thead><tr><th>函数名称</th><th>说明</th></tr></thead><tbody><tr><td>transpose</td><td>将数组的维度值进行对换，比如二维数组维度(2,4)使用该方法后为(4,2)。</td></tr><tr><td>ndarray.T</td><td>与 transpose 方法相同。</td></tr><tr><td>rollaxis</td><td>沿着指定的轴向后滚动至规定的位置。</td></tr><tr><td>swapaxes</td><td>对数组的轴进行对换。</td></tr></tbody></table><h3 id="numpy-transpose" tabindex="-1"><a class="header-anchor" href="#numpy-transpose" aria-hidden="true">#</a> numpy.transpose()</h3><p>用于对换多维数组的维度，比如二维数组使用此方法可以实现矩阵转置，语法格式如下：</p><p><code>numpy.transpose(arr, axes)</code></p><ul><li>arr：要操作的数组</li><li>axes：可选参数，元组或者整数列表，将会按照该参数进行转置。</li></ul><h3 id="numpy-rollaxis" tabindex="-1"><a class="header-anchor" href="#numpy-rollaxis" aria-hidden="true">#</a> numpy.rollaxis()</h3><p>沿着指定的轴，向后滚动至一个特定位置</p><p><code>numpy.rollaxis(arr, axis, start)</code></p><ul><li>arr：要传入的数组；</li><li>axis：沿着哪条轴向后滚动，其它轴的相对位置不会改变；</li><li>start：默认以 0 轴开始，可以根据数组维度调整它的值。</li></ul><h3 id="numpy-swapaxes" tabindex="-1"><a class="header-anchor" href="#numpy-swapaxes" aria-hidden="true">#</a> numpy.swapaxes()</h3><p>交换数组的两个轴</p><p><code>numpy.swapaxes(arr, axis1, axis2) </code></p><h2 id="修改数组维度操作" tabindex="-1"><a class="header-anchor" href="#修改数组维度操作" aria-hidden="true">#</a> 修改数组维度操作</h2><table><thead><tr><th>函数名称</th><th>描述说明</th></tr></thead><tbody><tr><td>broadcast</td><td>生成一个模拟广播的对象。</td></tr><tr><td>broadcast_to</td><td>将数组广播为新的形状。</td></tr><tr><td>expand_dims</td><td>扩展数组的形状。</td></tr><tr><td>squeeze</td><td>从数组的形状中删除一维项。</td></tr></tbody></table><h3 id="numpy-broadcast" tabindex="-1"><a class="header-anchor" href="#numpy-broadcast" aria-hidden="true">#</a> numpy.broadcast()</h3><p>返回值是数组被广播后的对象，该函数以两个数组作为输入参数</p><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
a <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
b <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">)</span> 
<span class="token comment"># 对b广播a</span>
d <span class="token operator">=</span> np<span class="token punctuation">.</span>broadcast<span class="token punctuation">(</span>a<span class="token punctuation">,</span>b<span class="token punctuation">)</span> 
<span class="token comment">#d它拥有 iterator 属性</span>
r<span class="token punctuation">,</span>c <span class="token operator">=</span> d<span class="token punctuation">.</span>iters
<span class="token keyword">print</span> <span class="token punctuation">(</span><span class="token builtin">next</span><span class="token punctuation">(</span>r<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token builtin">next</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token keyword">print</span> <span class="token punctuation">(</span><span class="token builtin">next</span><span class="token punctuation">(</span>r<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token builtin">next</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment"># 使用broadcast将a与b相加</span>
e <span class="token operator">=</span> np<span class="token punctuation">.</span>broadcast<span class="token punctuation">(</span>a<span class="token punctuation">,</span>b<span class="token punctuation">)</span>
f<span class="token operator">=</span>np<span class="token punctuation">.</span>empty<span class="token punctuation">(</span>e<span class="token punctuation">.</span>shape<span class="token punctuation">)</span>
f<span class="token punctuation">.</span>flat<span class="token operator">=</span><span class="token punctuation">[</span>x<span class="token operator">+</span>y <span class="token keyword">for</span> <span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">)</span> <span class="token keyword">in</span> e<span class="token punctuation">]</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>f<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>a<span class="token operator">+</span>b<span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="numpy-broadcast-to" tabindex="-1"><a class="header-anchor" href="#numpy-broadcast-to" aria-hidden="true">#</a> numpy.broadcast_to()</h3><p>将数组广播到新形状中，它在原始数组的基础上返回一个只读视图。 如果新形状不符合 NumPy 的广播规则，则会抛出 ValueError 异常。</p><p><code>numpy.broadcast_to(array, shape, subok)</code></p><div class="language-text line-numbers-mode" data-ext="text"><pre class="language-text"><code>import numpy as np
a = np.arange(4).reshape(1,4)
print(&quot;原数组&quot;,a)
print (&#39;调用 broadcast_to 函数之后：&#39;)
print (np.broadcast_to(a,(4,4)))
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="numpy-expand-dims" tabindex="-1"><a class="header-anchor" href="#numpy-expand-dims" aria-hidden="true">#</a> numpy.expand_dims()</h3><p>在指定位置插入新的轴，从而扩展数组的维度</p><p><code>numpy.expand_dims(arr, axis)</code></p><ul><li>arr：输入数组</li><li>axis：新轴插入的位置</li></ul><h4 id="numpy-squeeze" tabindex="-1"><a class="header-anchor" href="#numpy-squeeze" aria-hidden="true">#</a> numpy.squeeze()</h4><p>删除数组中维度为 1 的项</p><p><code>numpy.squeeze(arr, axis)</code></p><ul><li>arr：输入数的组；</li><li>axis：取值为整数或整数元组，用于指定需要删除的维度所在轴，指定的维度值必须为 1 ，否则将会报错，若为 None，则删除数组维度中所有为 1 的项。</li></ul><h2 id="连接与分割数组操作" tabindex="-1"><a class="header-anchor" href="#连接与分割数组操作" aria-hidden="true">#</a> 连接与分割数组操作</h2><table><caption> 连接与分割数组</caption><tbody><tr><th> 类型</th><th> 函数名称</th><th> 描述说明</th></tr><tr><td rowspan="4"> 连接数组方法</td><td> concatenate</td><td> 沿指定轴连接两个或者多个相同形状的数组</td></tr><tr><td> stack</td><td> 沿着新的轴连接一系列数组</td></tr><tr><td> hstack</td><td> 按水平顺序堆叠序列中数组（列方向）</td></tr><tr><td> vstack</td><td> 按垂直方向堆叠序列中数组（行方向）</td></tr><tr><td rowspan="3"> 分割数组方法</td><td> split</td><td> 将一个数组分割为多个子数组</td></tr><tr><td> hsplit</td><td> 将一个数组水平分割为多个子数组（按列）</td></tr><tr><td> vsplit</td><td> 将一个数组垂直分割为多个子数组（按行）</td></tr></tbody></table><h3 id="numpy-concatenate" tabindex="-1"><a class="header-anchor" href="#numpy-concatenate" aria-hidden="true">#</a> numpy.concatenate()</h3><p>沿指定轴连接相同形状的两个或多个数组，格式如下</p><p><code>numpy.concatenate((a1, a2, ...), axis)</code></p><p>参数说明：</p><ul><li>a1, a2, ...：表示一系列相同类型的数组；</li><li>axis：沿着该参数指定的轴连接数组，默认为 0。</li></ul><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
<span class="token comment">#创建数组a</span>
a <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token number">20</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">30</span><span class="token punctuation">,</span><span class="token number">40</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token keyword">print</span> <span class="token punctuation">(</span>a<span class="token punctuation">)</span>
<span class="token comment">#创建数组b</span>
b <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">50</span><span class="token punctuation">,</span><span class="token number">60</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">70</span><span class="token punctuation">,</span><span class="token number">80</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token keyword">print</span> <span class="token punctuation">(</span>b<span class="token punctuation">)</span>
<span class="token comment">#沿轴 0 连接两个数组</span>
<span class="token keyword">print</span> <span class="token punctuation">(</span>np<span class="token punctuation">.</span>concatenate<span class="token punctuation">(</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span>b<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment">#沿轴 1 连接两个数组</span>
<span class="token keyword">print</span> <span class="token punctuation">(</span>np<span class="token punctuation">.</span>concatenate<span class="token punctuation">(</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span>b<span class="token punctuation">)</span><span class="token punctuation">,</span>axis <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="numpy-split" tabindex="-1"><a class="header-anchor" href="#numpy-split" aria-hidden="true">#</a> numpy.split()</h3><p>沿指定的轴将数组分割为多个子数组</p><p><code>numpy.split(ary, indices_or_sections, axis)</code></p><ul><li>ary：被分割的数组</li><li>indices_or_sections：若是一个整数，代表用该整数平均切分，若是一个数组，则代表沿轴切分的位置（左开右闭）；</li><li>axis：默认为0，表示横向切分；为1时表示纵向切分。</li></ul><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
<span class="token comment">#arr1数组</span>
arr1 <span class="token operator">=</span> np<span class="token punctuation">.</span>floor<span class="token punctuation">(</span><span class="token number">10</span> <span class="token operator">*</span> np<span class="token punctuation">.</span>random<span class="token punctuation">.</span>random<span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>arr1<span class="token punctuation">)</span>
<span class="token comment">#拆分后数组</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>np<span class="token punctuation">.</span>hsplit<span class="token punctuation">(</span>arr1<span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><div class="language-text line-numbers-mode" data-ext="text"><pre class="language-text"><code>[array([[4., 1.],
       [3., 1.]]), array([[0., 5.],
       [5., 9.]]), array([[3., 6.],
       [7., 4.]])]
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="数组元素增删改查" tabindex="-1"><a class="header-anchor" href="#数组元素增删改查" aria-hidden="true">#</a> 数组元素增删改查</h2><table><thead><tr><th>函数名称</th><th>描述说明</th></tr></thead><tbody><tr><td>resize</td><td>返回指定形状的新数组。</td></tr><tr><td>append</td><td>将元素值添加到数组的末尾。</td></tr><tr><td>insert</td><td>沿规定的轴将元素值插入到指定的元素前。</td></tr><tr><td>delete</td><td>删掉某个轴上的子数组，并返回删除后的新数组。</td></tr><tr><td>argwhere</td><td>返回数组内符合条件的元素的索引值。</td></tr><tr><td>unique</td><td>用于删除数组中重复的元素，并按元素值由大到小返回一个新数组。</td></tr></tbody></table><h3 id="numpy-resize" tabindex="-1"><a class="header-anchor" href="#numpy-resize" aria-hidden="true">#</a> numpy.resize()</h3><p>返回指定形状的新数组。</p><p><code>numpy.resize(arr, shape)</code></p><p>resize 仅对原数组进行修改，没有返回值。</p><p>reshape 不仅对原数组进行修改，同时返回修改后的结果。</p><h3 id="numpy-append" tabindex="-1"><a class="header-anchor" href="#numpy-append" aria-hidden="true">#</a> numpy.append()</h3><p>在数组的末尾添加值，它返回一个一维数组，不修改原数组。</p><p><code>numpy.append(arr, values, axis=None)</code></p><ul><li>arr：输入的数组；</li><li>values：向 arr 数组中添加的值，需要和 arr 数组的形状保持一致；</li><li>axis：默认为 None，返回的是一维数组；当 axis =0 时，追加的值会被添加到行，而列数保持不变，若 axis=1 则与其恰好相反。</li></ul><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
a <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token comment">#向数组a添加元素</span>
<span class="token keyword">print</span> <span class="token punctuation">(</span>np<span class="token punctuation">.</span>append<span class="token punctuation">(</span>a<span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">8</span><span class="token punctuation">,</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

<span class="token comment">#沿轴 0 添加元素</span>
<span class="token keyword">print</span> <span class="token punctuation">(</span>np<span class="token punctuation">.</span>append<span class="token punctuation">(</span>a<span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">8</span><span class="token punctuation">,</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span>axis <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment">#沿轴 1 添加元素</span>
<span class="token keyword">print</span> <span class="token punctuation">(</span>np<span class="token punctuation">.</span>append<span class="token punctuation">(</span>a<span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">8</span><span class="token punctuation">,</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span>axis <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="numpy-insert" tabindex="-1"><a class="header-anchor" href="#numpy-insert" aria-hidden="true">#</a> numpy.insert()</h3><p>沿指定的轴，在给定索引值的前一个位置插入相应的值，如果没有提供轴，则输入数组被展开为一维数组。</p><p><code>numpy.insert(arr, obj, values, axis)</code></p><ul><li>arr：要输入的数组</li><li>obj：表示索引值，在该索引值之前插入 values 值；</li><li>values：要插入的值；</li><li>axis：指定的轴，如果未提供，则输入数组会被展开为一维数组。</li></ul><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
a <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>

<span class="token comment">#不提供axis的情况，会将数组展开</span>
<span class="token keyword">print</span> <span class="token punctuation">(</span>np<span class="token punctuation">.</span>insert<span class="token punctuation">(</span>a<span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">11</span><span class="token punctuation">,</span><span class="token number">12</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

<span class="token comment">#沿轴 0 垂直方向</span>
<span class="token keyword">print</span> <span class="token punctuation">(</span>np<span class="token punctuation">.</span>insert<span class="token punctuation">(</span>a<span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">11</span><span class="token punctuation">]</span><span class="token punctuation">,</span>axis <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

<span class="token comment">#沿轴 1 水平方向</span>
<span class="token keyword">print</span> <span class="token punctuation">(</span>np<span class="token punctuation">.</span>insert<span class="token punctuation">(</span>a<span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">11</span><span class="token punctuation">,</span>axis <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><div class="language-text line-numbers-mode" data-ext="text"><pre class="language-text"><code>[ 1  2  3 11 12  4  5  6]
[[ 1  2]
 [11 11]
 [ 3  4]
 [ 5  6]]
[[ 1 11  2]
 [ 3 11  4]
 [ 5 11  6]]
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="numpy-delete" tabindex="-1"><a class="header-anchor" href="#numpy-delete" aria-hidden="true">#</a> numpy.delete()</h3><p>从输入数组中删除指定的子数组，并返回一个新数组。它与 insert() 函数相似，若不提供 axis 参数，则输入数组被展开为一维数组。</p><p><code>numpy.delete(arr, obj, axis)</code></p><ul><li>arr：要输入的数组；</li><li>obj：整数或者整数数组，表示要被删除数组元素或者子数组；</li><li>axis：沿着哪条轴删除子数组。</li></ul><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
a <span class="token operator">=</span> np<span class="token punctuation">.</span>arange<span class="token punctuation">(</span><span class="token number">12</span><span class="token punctuation">)</span><span class="token punctuation">.</span>reshape<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">)</span>
<span class="token comment">#a数组</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span>
<span class="token comment">#不提供axis参数情况</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>np<span class="token punctuation">.</span>delete<span class="token punctuation">(</span>a<span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

<span class="token comment">#删除第二列</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>np<span class="token punctuation">.</span>delete<span class="token punctuation">(</span>a<span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span>axis <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

<span class="token comment">#删除经切片后的数组</span>
a <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">8</span><span class="token punctuation">,</span><span class="token number">9</span><span class="token punctuation">,</span><span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token keyword">print</span> <span class="token punctuation">(</span>np<span class="token punctuation">.</span>delete<span class="token punctuation">(</span>a<span class="token punctuation">,</span> np<span class="token punctuation">.</span>s_<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="numpy-argwhere" tabindex="-1"><a class="header-anchor" href="#numpy-argwhere" aria-hidden="true">#</a> numpy.argwhere()</h3><p>返回数组中非 0 元素的索引，若是多维数组则返回行、列索引组成的索引坐标。</p><div class="language-text line-numbers-mode" data-ext="text"><pre class="language-text"><code>import numpy as np
x = np.arange(6).reshape(2,3)
print(x)
#返回所有大于1的元素索引
y=np.argwhere(x&gt;1)
print(y)
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="numpy-unique" tabindex="-1"><a class="header-anchor" href="#numpy-unique" aria-hidden="true">#</a> numpy.unique()</h3><p>删除数组中重复的元素</p><p>numpy.unique(arr, return_index, return_inverse, return_counts)</p><ul><li>arr：输入数组，若是多维数组则以一维数组形式展开；</li><li>return_index：如果为 True，则返回新数组元素在原数组中的位置（索引）；</li><li>return_inverse：如果为 True，则返回原数组元素在新数组中的位置（索引）；</li><li>return_counts：如果为 True，则返回去重后的数组元素在原数组中出现的次数。</li></ul><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
a <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">,</span><span class="token number">8</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token keyword">print</span> <span class="token punctuation">(</span>a<span class="token punctuation">)</span>
<span class="token comment">#对a数组的去重</span>
uq <span class="token operator">=</span> np<span class="token punctuation">.</span>unique<span class="token punctuation">(</span>a<span class="token punctuation">)</span>
<span class="token keyword">print</span> <span class="token punctuation">(</span>uq<span class="token punctuation">)</span>

<span class="token comment">#数组去重后的索引数组</span>
u<span class="token punctuation">,</span>indices <span class="token operator">=</span> np<span class="token punctuation">.</span>unique<span class="token punctuation">(</span>a<span class="token punctuation">,</span> return_index <span class="token operator">=</span> <span class="token boolean">True</span><span class="token punctuation">)</span>
<span class="token comment">#打印去重后数组的索引</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>indices<span class="token punctuation">)</span>

<span class="token comment">#去重数组的下标：</span>
ui<span class="token punctuation">,</span>indices <span class="token operator">=</span> np<span class="token punctuation">.</span>unique<span class="token punctuation">(</span>a<span class="token punctuation">,</span>return_inverse <span class="token operator">=</span> <span class="token boolean">True</span><span class="token punctuation">)</span>
<span class="token keyword">print</span> <span class="token punctuation">(</span>ui<span class="token punctuation">)</span>
<span class="token comment">#打印下标</span>
<span class="token keyword">print</span> <span class="token punctuation">(</span>indices<span class="token punctuation">)</span>

<span class="token comment">#返回去重元素的重复数量</span>
uc<span class="token punctuation">,</span>indices <span class="token operator">=</span> np<span class="token punctuation">.</span>unique<span class="token punctuation">(</span>a<span class="token punctuation">,</span>return_counts <span class="token operator">=</span> <span class="token boolean">True</span><span class="token punctuation">)</span>
<span class="token keyword">print</span> <span class="token punctuation">(</span>uc<span class="token punctuation">)</span>
元素出现次数：
<span class="token keyword">print</span> <span class="token punctuation">(</span>indices<span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h1 id="位运算" tabindex="-1"><a class="header-anchor" href="#位运算" aria-hidden="true">#</a> 位运算</h1><table><thead><tr><th>序号</th><th>函数</th><th>位运算符</th><th>描述说明</th></tr></thead><tbody><tr><td>1</td><td>bitwise_and</td><td>&amp;</td><td>计算数组元素之间的按位与运算。</td></tr><tr><td>2</td><td>bitwise_or</td><td>|</td><td>计算数组元素之间的按位或运算。</td></tr><tr><td>3</td><td>invert</td><td>~</td><td>计算数组元素之间的按位取反运算。</td></tr><tr><td>4</td><td>left_shift</td><td>&lt;&lt;</td><td>将二进制数的位数向左移。</td></tr><tr><td>5</td><td>right_shift</td><td>&gt;&gt;</td><td>将二进制数的位数向右移。</td></tr></tbody></table><h3 id="bitwise-and" tabindex="-1"><a class="header-anchor" href="#bitwise-and" aria-hidden="true">#</a> bitwise_and()</h3><div class="language-text line-numbers-mode" data-ext="text"><pre class="language-text"><code>import numpy as np 
a = 10 
b = 12 
print(&quot;a的二进制数:&quot;,bin(a)) 
print(&quot;b的二进制数:&quot;,bin(b)) 
print(&quot;将a与b执行按位与操作:&quot;,np.bitwise_and(a,b))  
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="bitwise-or" tabindex="-1"><a class="header-anchor" href="#bitwise-or" aria-hidden="true">#</a> bitwise_or()</h3><div class="language-text line-numbers-mode" data-ext="text"><pre class="language-text"><code>import numpy as np
a,b = 13,17
print (&#39;13 和 17 的二进制数：&#39;)
print (bin(a), bin(b))

print (&#39;13 和 17 的位或：&#39;)
print (np.bitwise_or(13, 17))
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="invert" tabindex="-1"><a class="header-anchor" href="#invert" aria-hidden="true">#</a> Invert()</h2><p>取反，若是有符号的负整数，取其二进制数的补码，并执行 +1 操作。</p><div class="language-text line-numbers-mode" data-ext="text"><pre class="language-text"><code>import numpy as np 
#数据类型为无符号整型uint8
arr = np.array([20],dtype = np.uint8) 
print(&quot;二进制表示:&quot;,np.binary_repr(20,8)) 
print(np.invert(arr)) 
#进行取反操作
print(&quot;二进制表示: &quot;, np.binary_repr(235,8))  
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="left-shift" tabindex="-1"><a class="header-anchor" href="#left-shift" aria-hidden="true">#</a> left_shift()</h2><div class="language-text line-numbers-mode" data-ext="text"><pre class="language-text"><code>import numpy as np
#移动三位后的输出值
print (np.left_shift(20,3)
#打印移动后20的二进制数
print (np.binary_repr(20, width = 8))
#函数返回值的二进制数
print (np.binary_repr(160, width = 8))
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="right-shift" tabindex="-1"><a class="header-anchor" href="#right-shift" aria-hidden="true">#</a> right_shift()</h3><div class="language-text line-numbers-mode" data-ext="text"><pre class="language-text"><code>import numpy as np
#将40右移两位后返回值：
print (np.right_shift(40,2))
#移动后40的二进制数：
print (np.binary_repr(40, width = 8))
#移动后返回值的二进制数：
print (np.binary_repr(10, width = 8))
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="字符串处理函数" tabindex="-1"><a class="header-anchor" href="#字符串处理函数" aria-hidden="true">#</a> 字符串处理函数</h2><table><thead><tr><th>函数名称</th><th>描述</th></tr></thead><tbody><tr><td>add()</td><td>对两个数组相应位置的字符串做连接操作。</td></tr><tr><td>multiply()</td><td>返回多个字符串副本，比如将字符串“ hello”乘以3，则返回字符串“ hello hello hello”。</td></tr><tr><td>center()</td><td>用于居中字符串，并将指定的字符，填充在原字符串的左右两侧。</td></tr><tr><td>capitalize()</td><td>将字符串第一个字母转换为大写。</td></tr><tr><td>title()</td><td>标题样式，将每个字符串的第一个字母转换为大写形式。</td></tr><tr><td>lower()</td><td>将数组中所有的字符串的大写转换为小写。</td></tr><tr><td>upper()</td><td>将数组中所有的字符串的小写转换为大写。</td></tr><tr><td>split()</td><td>通过指定分隔符对字符串进行分割，并返回一个数组序列，默认分隔符为空格。</td></tr><tr><td>splitlines()</td><td>以换行符作为分隔符来分割字符串，并返回数组序列。</td></tr><tr><td>strip()</td><td>删除字符串开头和结尾处的空字符。</td></tr><tr><td>join()</td><td>返回一个新的字符串，该字符串是以指定分隔符来连接数组中的所有元素。</td></tr><tr><td>replace()</td><td>用新的字符串替换原数组中指定的字符串。</td></tr><tr><td>decode()</td><td>用指定的编码格式对数组中元素依次执行解码操作。</td></tr><tr><td>encode()</td><td>用指定的编码格式对数组中元素依次执行编码操作。</td></tr></tbody></table><h3 id="numpy-char-add" tabindex="-1"><a class="header-anchor" href="#numpy-char-add" aria-hidden="true">#</a> numpy.char.add()</h3><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np  
<span class="token keyword">print</span><span class="token punctuation">(</span>np<span class="token punctuation">.</span>char<span class="token punctuation">.</span>add<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">&#39;welcome&#39;</span><span class="token punctuation">,</span><span class="token string">&#39;url&#39;</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string">&#39; to C net&#39;</span><span class="token punctuation">,</span><span class="token string">&#39;is c.biancheng.net&#39;</span><span class="token punctuation">]</span> <span class="token punctuation">)</span><span class="token punctuation">)</span>  
<span class="token punctuation">[</span><span class="token string">&#39;welcome to C net&#39;</span> <span class="token string">&#39;url is c.biancheng.net&#39;</span><span class="token punctuation">]</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="数学函数" tabindex="-1"><a class="header-anchor" href="#数学函数" aria-hidden="true">#</a> 数学函数</h2><h3 id="三角函数" tabindex="-1"><a class="header-anchor" href="#三角函数" aria-hidden="true">#</a> 三角函数</h3><div class="language-text line-numbers-mode" data-ext="text"><pre class="language-text"><code>import numpy as np 
arr = np.array([0, 30, 60, 90, 120, 150, 180]) 
#计算arr数组中给定角度的三角函数值
#通过乘以np.pi/180将其转换为弧度
print(np.sin(arr * np.pi/180)) 
print(np.cos(arr * np.pi/180)) 
print(np.tan(arr * np.pi/180))  
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="舍入函数" tabindex="-1"><a class="header-anchor" href="#舍入函数" aria-hidden="true">#</a> 舍入函数</h2><h3 id="numpy-around" tabindex="-1"><a class="header-anchor" href="#numpy-around" aria-hidden="true">#</a> numpy.around()</h3><p>返回一个十进制值数，并将数值四舍五入到指定的小数位上。</p><p><code>numpy.around(a,decimals)</code></p><ul><li>a：代表要输入的数组；</li><li>decimals：要舍入到的小数位数。它的默认值为0，如果为负数，则小数点将移到整数左侧。</li></ul><h3 id="numpy-floor" tabindex="-1"><a class="header-anchor" href="#numpy-floor" aria-hidden="true">#</a> numpy.floor()</h3><p>对数组中的每个元素向下取整数</p><h3 id="numpy-ceil" tabindex="-1"><a class="header-anchor" href="#numpy-ceil" aria-hidden="true">#</a> numpy.ceil()</h3><p>向上取整</p><h2 id="算术运算" tabindex="-1"><a class="header-anchor" href="#算术运算" aria-hidden="true">#</a> 算术运算</h2><div class="language-text line-numbers-mode" data-ext="text"><pre class="language-text"><code>import numpy as np
a = np.arange(9, dtype = np.float_).reshape(3,3)
#数组a
print(a)
#数组b
b = np.array([10,10,10])
print(b)
#数组加法运算
print(np.add(a,b))
#数组减法运算
print(np.subtract(a,b))
#数组乘法运算
print(np.multiply(a,b))
#数组除法运算
print(np.divide(a,b))
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="numpy-reciprocal" tabindex="-1"><a class="header-anchor" href="#numpy-reciprocal" aria-hidden="true">#</a> numpy.reciprocal()</h3><p>对数组中的每个元素取倒数，并以数组的形式将它们返回。</p><h3 id="numpy-power" tabindex="-1"><a class="header-anchor" href="#numpy-power" aria-hidden="true">#</a> numpy.power()</h3><h3 id="numpy-mod" tabindex="-1"><a class="header-anchor" href="#numpy-mod" aria-hidden="true">#</a> numpy.mod()</h3><p>返回两个数组相对应位置上元素相除后的余数，它与 numpy.remainder() 的作用相同 。</p><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
a <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">11</span><span class="token punctuation">,</span><span class="token number">22</span><span class="token punctuation">,</span><span class="token number">33</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
b <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token comment">#a与b相应位置的元素做除法</span>
<span class="token keyword">print</span><span class="token punctuation">(</span> np<span class="token punctuation">.</span>mod<span class="token punctuation">(</span>a<span class="token punctuation">,</span>b<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment">#remainder方法一样</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>np<span class="token punctuation">.</span>remainder<span class="token punctuation">(</span>a<span class="token punctuation">,</span>b<span class="token punctuation">)</span><span class="token punctuation">)</span> 
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="复数数组处理函数" tabindex="-1"><a class="header-anchor" href="#复数数组处理函数" aria-hidden="true">#</a> 复数数组处理函数</h3><ul><li>numpy.real() 返回复数数组的实部；</li><li>numpy.imag() 返回复数数组的虚部；</li><li>numpy.conj() 通过更改虚部的符号，从而返回共轭复数；</li><li>numpy.angle() 返回复数参数的角度，该函数的提供了一个 deg 参数，如果 deg=True，则返回的值会以角度制来表示，否则以以弧度制来表示。</li></ul><h2 id="统计函数" tabindex="-1"><a class="header-anchor" href="#统计函数" aria-hidden="true">#</a> 统计函数</h2><h3 id="numpy-amin-和-numpy-amax" tabindex="-1"><a class="header-anchor" href="#numpy-amin-和-numpy-amax" aria-hidden="true">#</a> numpy.amin() 和 numpy.amax()</h3><ul><li>amin() 沿指定的轴，查找数组中元素的最小值，并以数组形式返回；</li><li>amax() 沿指定的轴，查找数组中元素的最大值，并以数组形式返回。</li></ul><h3 id="numpy-ptp" tabindex="-1"><a class="header-anchor" href="#numpy-ptp" aria-hidden="true">#</a> numpy.ptp()</h3><p>用于计算数组元素中最值之差值，也就是（最大值 - 最小值）。</p><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np 
a <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token number">20</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">80</span><span class="token punctuation">,</span><span class="token number">43</span><span class="token punctuation">,</span><span class="token number">31</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">22</span><span class="token punctuation">,</span><span class="token number">43</span><span class="token punctuation">,</span><span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span> 
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;原数组&quot;</span><span class="token punctuation">,</span>a<span class="token punctuation">)</span> 
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;沿着axis 1:&quot;</span><span class="token punctuation">,</span>np<span class="token punctuation">.</span>ptp<span class="token punctuation">(</span>a<span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span> 
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;沿着axis 0:&quot;</span><span class="token punctuation">,</span>np<span class="token punctuation">.</span>ptp<span class="token punctuation">(</span>a<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">)</span> 
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="numpy-percentile" tabindex="-1"><a class="header-anchor" href="#numpy-percentile" aria-hidden="true">#</a> numpy.percentile()</h3><p>百分位数，是统计学中使用的一种度量单位。该函数表示沿指定轴，计算数组中任意百分比分位数</p><p><code>numpy.percentile(a, q, axis)</code></p><ul><li>a：输入数组；</li><li>q：要计算的百分位数，在 0~100 之间；</li><li>axis：沿着指定的轴计算百分位数。</li></ul><p><a href="https://zhuanlan.zhihu.com/p/469520724?utm_id=0" target="_blank" rel="noopener noreferrer">numpy.percentile()函数整理<span><svg class="external-link-icon" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg><span class="external-link-icon-sr-only">open in new window</span></span></a></p><h3 id="numpy-median" tabindex="-1"><a class="header-anchor" href="#numpy-median" aria-hidden="true">#</a> numpy.median()</h3><p>计算 a 数组元素的中位数（中值）</p><h3 id="numpy-mean" tabindex="-1"><a class="header-anchor" href="#numpy-mean" aria-hidden="true">#</a> numpy.mean()</h3><p>计算数组中元素的算术平均值（即元素之总和除以元素数量）。</p><h3 id="numpy-average" tabindex="-1"><a class="header-anchor" href="#numpy-average" aria-hidden="true">#</a> numpy.average()</h3><p>加权平均值是将数组中各数值乘以相应的权数，然后再对权重值求总和，最后以权重的总和除以总的单位数（即因子个数）。</p><p>numpy.average() 根据在数组中给出的权重，计算数组元素的加权平均值。该函数可以接受一个轴参数 axis，如果未指定，则数组被展开为一维数组。</p><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
a <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">)</span> 
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&#39;a数组是：&#39;</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span>
<span class="token comment">#average()函数：</span>
<span class="token keyword">print</span> <span class="token punctuation">(</span>np<span class="token punctuation">.</span>average<span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment"># 若不指定权重相当于对数组求均值</span>
we <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span> 
<span class="token comment">#调用 average() 函数：&#39;)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>np<span class="token punctuation">.</span>average<span class="token punctuation">(</span>a<span class="token punctuation">,</span>weights <span class="token operator">=</span> we<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment">#returned 为Ture，则返回权重的和 </span>
prin<span class="token punctuation">(</span>np<span class="token punctuation">.</span>average<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">,</span>weights <span class="token operator">=</span>  <span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> returned <span class="token operator">=</span>  <span class="token boolean">True</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="np-var" tabindex="-1"><a class="header-anchor" href="#np-var" aria-hidden="true">#</a> np.var()</h3><p>方差</p><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
<span class="token keyword">print</span> <span class="token punctuation">(</span>np<span class="token punctuation">.</span>var<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="np-std" tabindex="-1"><a class="header-anchor" href="#np-std" aria-hidden="true">#</a> np.std()</h3><p>标准差</p><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
<span class="token keyword">print</span> <span class="token punctuation">(</span>np<span class="token punctuation">.</span>std<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="排序和搜索" tabindex="-1"><a class="header-anchor" href="#排序和搜索" aria-hidden="true">#</a> 排序和搜索</h2><h3 id="numpy-sort" tabindex="-1"><a class="header-anchor" href="#numpy-sort" aria-hidden="true">#</a> numpy.sort()</h3><p><code>numpy.sort(a, axis, kind, order)</code></p><ul><li>a：要排序的数组；</li><li>axis：沿着指定轴进行排序，如果没有指定 axis，默认在最后一个轴上排序，若 axis=0 表示按列排序，axis=1 表示按行排序；</li><li>kind：默认为 quicksort（快速排序）；</li><li>order：若数组设置了字段，则 order 表示要排序的字段。</li></ul><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np 
a <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">9</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span> 
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&#39;a数组是：&#39;</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span>
<span class="token comment">#调用sort()函数</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>np<span class="token punctuation">.</span>sort<span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment">#按列排序：</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>np<span class="token punctuation">.</span>sort<span class="token punctuation">(</span>a<span class="token punctuation">,</span> axis <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment">#设置在sort函数中排序字段</span>
dt <span class="token operator">=</span> np<span class="token punctuation">.</span>dtype<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">(</span><span class="token string">&#39;name&#39;</span><span class="token punctuation">,</span>  <span class="token string">&#39;S10&#39;</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token string">&#39;age&#39;</span><span class="token punctuation">,</span>  <span class="token builtin">int</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
a <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">(</span><span class="token string">&quot;raju&quot;</span><span class="token punctuation">,</span><span class="token number">21</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token string">&quot;anil&quot;</span><span class="token punctuation">,</span><span class="token number">25</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token string">&quot;ravi&quot;</span><span class="token punctuation">,</span>  <span class="token number">17</span><span class="token punctuation">)</span><span class="token punctuation">,</span>  <span class="token punctuation">(</span><span class="token string">&quot;amar&quot;</span><span class="token punctuation">,</span><span class="token number">27</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">,</span> dtype <span class="token operator">=</span> dt<span class="token punctuation">)</span> 
<span class="token comment">#再次打印a数组</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span>
<span class="token comment">#按name字段排序</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>np<span class="token punctuation">.</span>sort<span class="token punctuation">(</span>a<span class="token punctuation">,</span> order <span class="token operator">=</span> <span class="token string">&#39;name&#39;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="numpy-argsort" tabindex="-1"><a class="header-anchor" href="#numpy-argsort" aria-hidden="true">#</a> numpy.argsort()</h3><p>沿着指定的轴，对输入数组的元素值进行排序，并返回排序后的元素索引数组。</p><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np 
a <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">90</span><span class="token punctuation">,</span> <span class="token number">29</span><span class="token punctuation">,</span> <span class="token number">89</span><span class="token punctuation">,</span> <span class="token number">12</span><span class="token punctuation">]</span><span class="token punctuation">)</span> 
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;原数组&quot;</span><span class="token punctuation">,</span>a<span class="token punctuation">)</span> 
sort_ind <span class="token operator">=</span> np<span class="token punctuation">.</span>argsort<span class="token punctuation">(</span>a<span class="token punctuation">)</span> 
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;打印排序元素索引值&quot;</span><span class="token punctuation">,</span>sort_ind<span class="token punctuation">)</span> 
<span class="token comment">#使用索引数组对原数组排序</span>
sort_a <span class="token operator">=</span> a<span class="token punctuation">[</span>sort_ind<span class="token punctuation">]</span> 
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;打印排序数组&quot;</span><span class="token punctuation">)</span> 
<span class="token keyword">for</span> i <span class="token keyword">in</span> sort_ind<span class="token punctuation">:</span> 
    <span class="token keyword">print</span><span class="token punctuation">(</span>a<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span>end <span class="token operator">=</span> <span class="token string">&quot; &quot;</span><span class="token punctuation">)</span>  
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="numpy-lexsort" tabindex="-1"><a class="header-anchor" href="#numpy-lexsort" aria-hidden="true">#</a> numpy.lexsort()</h3><p>按键序列对数组进行排序，它返回一个已排序的索引数组，类似于 numpy.argsort()</p><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np 
a <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">&#39;a&#39;</span><span class="token punctuation">,</span><span class="token string">&#39;b&#39;</span><span class="token punctuation">,</span><span class="token string">&#39;c&#39;</span><span class="token punctuation">,</span><span class="token string">&#39;d&#39;</span><span class="token punctuation">,</span><span class="token string">&#39;e&#39;</span><span class="token punctuation">]</span><span class="token punctuation">)</span> 
b <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">12</span><span class="token punctuation">,</span> <span class="token number">90</span><span class="token punctuation">,</span> <span class="token number">380</span><span class="token punctuation">,</span> <span class="token number">12</span><span class="token punctuation">,</span> <span class="token number">211</span><span class="token punctuation">]</span><span class="token punctuation">)</span> 
ind <span class="token operator">=</span> np<span class="token punctuation">.</span>lexsort<span class="token punctuation">(</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span>b<span class="token punctuation">)</span><span class="token punctuation">)</span> 
<span class="token comment">#打印排序元素的索引数组</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>ind<span class="token punctuation">)</span> 
<span class="token comment">#使用索引数组对数组进行排序</span>
<span class="token keyword">for</span> i <span class="token keyword">in</span> ind<span class="token punctuation">:</span> 
    <span class="token keyword">print</span><span class="token punctuation">(</span>a<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span>b<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span>  
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><div class="language-text line-numbers-mode" data-ext="text"><pre class="language-text"><code>打印排序元素的索引数组：
[0 3 1 4 2]
使用索引数组对原数组进行排序：
a 12
d 12
b 90
e 211
c 380
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="numpy-nonzero" tabindex="-1"><a class="header-anchor" href="#numpy-nonzero" aria-hidden="true">#</a> numpy.nonzero()</h3><p>从数组中查找非零元素的索引位置。</p><h2 id="numpy-where" tabindex="-1"><a class="header-anchor" href="#numpy-where" aria-hidden="true">#</a> numpy.where()</h2><p>返回值是满足了给定条件的元素索引值。</p><div class="language-text line-numbers-mode" data-ext="text"><pre class="language-text"><code>import numpy as np 
 
b = np.array([12, 90, 380, 12, 211]) 
 
print(np.where(b&gt;12)) 
 
c = np.array([[20, 24],[21, 23]]) 
 
print(np.where(c&gt;20))  
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="numpy-extract" tabindex="-1"><a class="header-anchor" href="#numpy-extract" aria-hidden="true">#</a> numpy.extract()</h3><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
x <span class="token operator">=</span> np<span class="token punctuation">.</span>arange<span class="token punctuation">(</span><span class="token number">9.</span><span class="token punctuation">)</span><span class="token punctuation">.</span>reshape<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span>
打印数组x<span class="token punctuation">:</span>&#39;
<span class="token keyword">print</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span> 
<span class="token comment">#设置条件选择偶数元素</span>
condition <span class="token operator">=</span> np<span class="token punctuation">.</span>mod<span class="token punctuation">(</span>x<span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token operator">==</span> <span class="token number">0</span>
<span class="token comment">#输出布尔值数组</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>condition<span class="token punctuation">)</span>
<span class="token comment">#按condition提取满足条件的元素值</span>
<span class="token keyword">print</span> np<span class="token punctuation">.</span>extract<span class="token punctuation">(</span>condition<span class="token punctuation">,</span> x<span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code>a数组是：
<span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">0.</span> <span class="token number">1.</span> <span class="token number">2.</span><span class="token punctuation">]</span>
<span class="token punctuation">[</span><span class="token number">3.</span> <span class="token number">4.</span> <span class="token number">5.</span><span class="token punctuation">]</span>
<span class="token punctuation">[</span><span class="token number">6.</span> <span class="token number">7.</span> <span class="token number">8.</span><span class="token punctuation">]</span><span class="token punctuation">]</span>
输出布尔值数组：
<span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token boolean">True</span> <span class="token boolean">False</span>  <span class="token boolean">True</span><span class="token punctuation">]</span>
<span class="token punctuation">[</span><span class="token boolean">False</span>  <span class="token boolean">True</span> <span class="token boolean">False</span><span class="token punctuation">]</span>
<span class="token punctuation">[</span> <span class="token boolean">True</span> <span class="token boolean">False</span>  <span class="token boolean">True</span><span class="token punctuation">]</span><span class="token punctuation">]</span>
按条件提取元素：
<span class="token punctuation">[</span><span class="token number">0.</span> <span class="token number">2.</span> <span class="token number">4.</span> <span class="token number">6.</span> <span class="token number">8.</span><span class="token punctuation">]</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="numpy-argmax" tabindex="-1"><a class="header-anchor" href="#numpy-argmax" aria-hidden="true">#</a> numpy.argmax()</h2><p>返回最大值的的索引</p><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
a <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">30</span><span class="token punctuation">,</span><span class="token number">40</span><span class="token punctuation">,</span><span class="token number">70</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">80</span><span class="token punctuation">,</span><span class="token number">20</span><span class="token punctuation">,</span><span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">50</span><span class="token punctuation">,</span><span class="token number">90</span><span class="token punctuation">,</span><span class="token number">60</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span> 
<span class="token comment">#a数组</span>
<span class="token keyword">print</span> <span class="token punctuation">(</span>a<span class="token punctuation">)</span>
<span class="token comment">#argmax() 函数</span>
<span class="token keyword">print</span> <span class="token punctuation">(</span>np<span class="token punctuation">.</span>argmax<span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment">#将数组以一维展开</span>
<span class="token keyword">print</span> <span class="token punctuation">(</span>a<span class="token punctuation">.</span>flatten<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment">#沿轴 0 的最大值索引：</span>
maxindex <span class="token operator">=</span> np<span class="token punctuation">.</span>argmax<span class="token punctuation">(</span>a<span class="token punctuation">,</span> axis <span class="token operator">=</span>  <span class="token number">0</span><span class="token punctuation">)</span> 
<span class="token keyword">print</span> <span class="token punctuation">(</span>maxindex<span class="token punctuation">)</span>
<span class="token comment">#沿轴 1 的最大值索引</span>
maxindex <span class="token operator">=</span> np<span class="token punctuation">.</span>argmax<span class="token punctuation">(</span>a<span class="token punctuation">,</span> axis <span class="token operator">=</span>  <span class="token number">1</span><span class="token punctuation">)</span> 
<span class="token keyword">print</span> <span class="token punctuation">(</span>maxindex<span class="token punctuation">)</span> 
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="numpy-argmin" tabindex="-1"><a class="header-anchor" href="#numpy-argmin" aria-hidden="true">#</a> numpy.argmin()</h2><p>求最小值索引。</p><h2 id="副本和视图" tabindex="-1"><a class="header-anchor" href="#副本和视图" aria-hidden="true">#</a> 副本和视图</h2><h3 id="ndarray-view" tabindex="-1"><a class="header-anchor" href="#ndarray-view" aria-hidden="true">#</a> ndarray.view()</h3><p>返回一个新生成的数组视图，因此对该数组的操作，会影响到原数组。</p><h2 id="字节交换" tabindex="-1"><a class="header-anchor" href="#字节交换" aria-hidden="true">#</a> 字节交换</h2><h3 id="numpy-ndarray-byteswap" tabindex="-1"><a class="header-anchor" href="#numpy-ndarray-byteswap" aria-hidden="true">#</a> numpy.ndarray.byteswap()</h3><p>将数组中每个元素的字节顺序进行大小端调换。</p><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
a <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">256</span><span class="token punctuation">,</span> <span class="token number">8755</span><span class="token punctuation">]</span><span class="token punctuation">,</span> dtype <span class="token operator">=</span> np<span class="token punctuation">.</span>int16<span class="token punctuation">)</span>
<span class="token comment">#数组a</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span> 
<span class="token comment">#以16进制形式表示内存中的数据</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token builtin">map</span><span class="token punctuation">(</span><span class="token builtin">hex</span><span class="token punctuation">,</span>a<span class="token punctuation">)</span><span class="token punctuation">)</span> 
<span class="token comment">#byteswap()函数通过传递True参数在适当的位置进行转换</span>
<span class="token comment">#调用byteswap()函数</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>a<span class="token punctuation">.</span>byteswap<span class="token punctuation">(</span><span class="token boolean">True</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment">#十六进制形式</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token builtin">map</span><span class="token punctuation">(</span><span class="token builtin">hex</span><span class="token punctuation">,</span>a<span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="matrix矩阵库" tabindex="-1"><a class="header-anchor" href="#matrix矩阵库" aria-hidden="true">#</a> Matrix矩阵库</h2><p>该模块中的函数返回的是一个 matrix 对象，而非 ndarray 对象。</p><h3 id="matlib-empty" tabindex="-1"><a class="header-anchor" href="#matlib-empty" aria-hidden="true">#</a> matlib.empty()</h3><p>返回一个空矩阵，所以它的创建速度非常快。</p><p><code>numpy.matlib.empty(shape, dtype, order)</code></p><ul><li>shape：以元组的形式指定矩阵的形状。</li><li>dtype：表示矩阵的数据类型。</li><li>order：有两种选择，C（行序优先） 或者 F（列序优先）。</li><li>矩阵中会填充无意义的随机值</li></ul><h3 id="numpy-matlib-zeros" tabindex="-1"><a class="header-anchor" href="#numpy-matlib-zeros" aria-hidden="true">#</a> numpy.matlib.zeros()</h3><p>创建一个以 0 填充的矩阵</p><p><code>np.matlib.zeros((2,2))</code></p><h3 id="numpy-matlib-ones" tabindex="-1"><a class="header-anchor" href="#numpy-matlib-ones" aria-hidden="true">#</a> numpy.matlib.ones()</h3><p>创建一个以 1 填充的矩阵。</p><h2 id="numpy-matlib-eye" tabindex="-1"><a class="header-anchor" href="#numpy-matlib-eye" aria-hidden="true">#</a> numpy.matlib.eye()</h2><p>返回一个对角线元素为 1，而其他元素为 0 的矩阵 。</p><p><code>numpy.matlib.eye(n,M,k, dtype)</code></p><ul><li>n：返回矩阵的行数；</li><li>M：返回矩阵的列数，默认为 n；</li><li>k：对角线的索引；</li><li>dtype：矩阵中元素数据类型。</li></ul><h3 id="numpy-matlib-identity" tabindex="-1"><a class="header-anchor" href="#numpy-matlib-identity" aria-hidden="true">#</a> numpy.matlib.identity()</h3><p>返回一个给定大小的单位矩阵，矩阵的对角线元素为 1，而其他元素均为 0。</p><h3 id="numpy-matlib-rand" tabindex="-1"><a class="header-anchor" href="#numpy-matlib-rand" aria-hidden="true">#</a> numpy.matlib.rand()</h3><p>创建一个以随机数填充，并给定维度的矩阵。</p><p><code>np.matlib.rand(3,3)</code></p><p>因为 matrix 只能表示二维数据，而 ndarray 也可以是二维数组，所以两者可以互相转换。</p><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token comment">#创建矩阵i</span>
<span class="token keyword">import</span> numpy<span class="token punctuation">.</span>matlib
<span class="token keyword">import</span> numpy <span class="token keyword">as</span> np 
i <span class="token operator">=</span> np<span class="token punctuation">.</span>matrix<span class="token punctuation">(</span><span class="token string">&#39;1,2;3,4&#39;</span><span class="token punctuation">)</span> 
<span class="token keyword">print</span> <span class="token punctuation">(</span>i<span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>相互转化</p><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token keyword">import</span> numpy<span class="token punctuation">.</span>matlib
<span class="token keyword">import</span> numpy <span class="token keyword">as</span> np 
j <span class="token operator">=</span> np<span class="token punctuation">.</span>asarray<span class="token punctuation">(</span>i<span class="token punctuation">)</span> 
<span class="token keyword">print</span> <span class="token punctuation">(</span>j<span class="token punctuation">)</span>
k <span class="token operator">=</span> np<span class="token punctuation">.</span>asmatrix <span class="token punctuation">(</span>j<span class="token punctuation">)</span>
<span class="token keyword">print</span> <span class="token punctuation">(</span>k<span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="线性代数" tabindex="-1"><a class="header-anchor" href="#线性代数" aria-hidden="true">#</a> 线性代数</h2><p>NumPy 提供了 numpy.linalg 模块，该模块中包含了一些常用的线性代数计算方法</p><table><thead><tr><th>函数名称</th><th>描述说明</th></tr></thead><tbody><tr><td>dot</td><td>两个数组的点积。</td></tr><tr><td>vdot</td><td>两个向量的点积。</td></tr><tr><td>inner</td><td>两个数组的内积。</td></tr><tr><td>matmul</td><td>两个数组的矩阵积。</td></tr><tr><td>det</td><td>计算输入矩阵的行列式。</td></tr><tr><td>solve</td><td>求解线性矩阵方程。</td></tr><tr><td>inv</td><td>计算矩阵的逆矩阵，逆矩阵与原始矩阵相乘，会得到单位矩阵。</td></tr></tbody></table><h2 id="numpy-dot" tabindex="-1"><a class="header-anchor" href="#numpy-dot" aria-hidden="true">#</a> numpy.dot()</h2><p>按照矩阵的乘法规则，计算两个矩阵的点积运算结果。</p><div class="language-text line-numbers-mode" data-ext="text"><pre class="language-text"><code>A=[1,2,3]
B=[4,5,6]
print(np.dot(A,B)) #32
a = np.array([[100,200],
             [23,12]])
b = np.array([[10,20],
            [12,21]])
dot = np.dot(a,b)
print(dot)  #[[3400 6200]
            #[ 374  712]]
# [[100*10+200*12,100*20+200*21]
# [23*10+12*12,23*20+12*21]]
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="numpy-vdot" tabindex="-1"><a class="header-anchor" href="#numpy-vdot" aria-hidden="true">#</a> numpy.vdot()</h3><p>用于计算两个<strong>向量</strong>的点积结果，与 dot() 函数不同。</p><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np 
a <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">100</span><span class="token punctuation">,</span><span class="token number">200</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">23</span><span class="token punctuation">,</span><span class="token number">12</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span> 
b <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token number">20</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">12</span><span class="token punctuation">,</span><span class="token number">21</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span> 
vdot <span class="token operator">=</span> np<span class="token punctuation">.</span>vdot<span class="token punctuation">(</span>a<span class="token punctuation">,</span>b<span class="token punctuation">)</span> 
<span class="token keyword">print</span><span class="token punctuation">(</span>vdot<span class="token punctuation">)</span>  <span class="token comment">#5528</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="numpy-inner" tabindex="-1"><a class="header-anchor" href="#numpy-inner" aria-hidden="true">#</a> numpy.inner()</h3><p>用于计算数组之间的内积。当计算的数组是一维数组时，它与 dot() 函数相同，若输入的是多维数组则两者存在不同。</p><h3 id="numpy-matmul" tabindex="-1"><a class="header-anchor" href="#numpy-matmul" aria-hidden="true">#</a> numpy.matmul()</h3><p>返回两个矩阵的乘积，假如两个矩阵的维度不一致，就会产生错误。</p><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np 
a <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">8</span><span class="token punctuation">,</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span> 
b <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">23</span><span class="token punctuation">,</span><span class="token number">23</span><span class="token punctuation">,</span><span class="token number">12</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">8</span><span class="token punctuation">,</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span> 
mul <span class="token operator">=</span> np<span class="token punctuation">.</span>matmul<span class="token punctuation">(</span>a<span class="token punctuation">,</span>b<span class="token punctuation">)</span> 
<span class="token keyword">print</span><span class="token punctuation">(</span>mul<span class="token punctuation">)</span>  
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="numpy-linalg-det" tabindex="-1"><a class="header-anchor" href="#numpy-linalg-det" aria-hidden="true">#</a> numpy.linalg.det()</h3><p>使用对角线元素来计算矩阵的行列式.</p><p><code>np.linalg.det(a)</code></p><h2 id="numpy-linalg-solve" tabindex="-1"><a class="header-anchor" href="#numpy-linalg-solve" aria-hidden="true">#</a> numpy.linalg.solve()</h2><p>用于求解线性矩阵方程组，并以矩阵的形式表示线性方程的解</p><div class="language-text line-numbers-mode" data-ext="text"><pre class="language-text"><code>3X  +  2 Y + Z =  10  
X + Y + Z = 6
X + 2Y - Z = 2
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><div class="language-text line-numbers-mode" data-ext="text"><pre class="language-text"><code>方程系数矩阵：
3   2   1 
1   1   1 
1   2  -1
方程变量矩阵:
X 
Y 
Z  
方程结果矩阵：
10 
6
2
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
m <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token keyword">print</span> <span class="token punctuation">(</span><span class="token string">&#39;数组 m：&#39;</span><span class="token punctuation">)</span>
<span class="token keyword">print</span> <span class="token punctuation">(</span>m<span class="token punctuation">)</span>
<span class="token keyword">print</span> <span class="token punctuation">(</span><span class="token string">&#39;矩阵 n：&#39;</span><span class="token punctuation">)</span>
n <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token keyword">print</span> <span class="token punctuation">(</span>n<span class="token punctuation">)</span>
<span class="token keyword">print</span> <span class="token punctuation">(</span><span class="token string">&#39;计算：m^(-1)n：&#39;</span><span class="token punctuation">)</span>
x <span class="token operator">=</span> np<span class="token punctuation">.</span>linalg<span class="token punctuation">.</span>solve<span class="token punctuation">(</span>m<span class="token punctuation">,</span>n<span class="token punctuation">)</span>
<span class="token keyword">print</span> <span class="token punctuation">(</span>x<span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code>x为线性方程的解：
<span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1.</span><span class="token punctuation">]</span>
<span class="token punctuation">[</span><span class="token number">2.</span><span class="token punctuation">]</span>
<span class="token punctuation">[</span><span class="token number">3.</span><span class="token punctuation">]</span><span class="token punctuation">]</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="numpy-linalg-inv" tabindex="-1"><a class="header-anchor" href="#numpy-linalg-inv" aria-hidden="true">#</a> numpy.linalg.inv()</h3><p>计算矩阵的逆矩阵，逆矩阵与原矩阵相乘得到单位矩阵。</p><div class="language-text line-numbers-mode" data-ext="text"><pre class="language-text"><code>import numpy as np 
a = np.array([[1,2],[3,4]]) 
print(&quot;原数组:&quot;,a) 
b = np.linalg.inv(a) 
print(&quot;求逆:&quot;,b)  
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="矩阵乘法" tabindex="-1"><a class="header-anchor" href="#矩阵乘法" aria-hidden="true">#</a> 矩阵乘法</h2><h3 id="multiple" tabindex="-1"><a class="header-anchor" href="#multiple" aria-hidden="true">#</a> multiple()</h3><p>逐元素矩阵乘法</p><div class="language-text line-numbers-mode" data-ext="text"><pre class="language-text"><code>import numpy as np 
array1=np.array([[1,2,3],[4,5,6],[7,8,9]],ndmin=3) 
array2=np.array([[9,8,7],[6,5,4],[3,2,1]],ndmin=3) 
result=np.multiply(array1,array2) 
result  
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><div class="language-text line-numbers-mode" data-ext="text"><pre class="language-text"><code>array([[[ 9, 16, 21],
         [24, 25, 24],
         [21, 16,  9]]])
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="matmul" tabindex="-1"><a class="header-anchor" href="#matmul" aria-hidden="true">#</a> matmul()</h3><p>矩阵乘积运算</p><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np 
array1<span class="token operator">=</span>np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">8</span><span class="token punctuation">,</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span>ndmin<span class="token operator">=</span><span class="token number">3</span><span class="token punctuation">)</span> 
array2<span class="token operator">=</span>np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">9</span><span class="token punctuation">,</span><span class="token number">8</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">6</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span>ndmin<span class="token operator">=</span><span class="token number">3</span><span class="token punctuation">)</span> 
result<span class="token operator">=</span>np<span class="token punctuation">.</span>matmul<span class="token punctuation">(</span>array1<span class="token punctuation">,</span>array2<span class="token punctuation">)</span> 
<span class="token keyword">print</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span> 
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><div class="language-text line-numbers-mode" data-ext="text"><pre class="language-text"><code>数组（[[[
         [30，24，18]，
         [84，69，54 ]，[138，114，90]]]）
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="dot" tabindex="-1"><a class="header-anchor" href="#dot" aria-hidden="true">#</a> dot()</h3><p>函数用于计算两个矩阵的点积。</p><div class="language-text line-numbers-mode" data-ext="text"><pre class="language-text"><code>import numpy as np 
array1=np.array([[1,2,3],[4,5,6],[7,8,9]],ndmin=3) 
array2=np.array([[9,8,7],[6,5,4],[3,2,1]],ndmin=3) 
result=np.dot(array1,array2) 
print(result)  
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><div class="language-text line-numbers-mode" data-ext="text"><pre class="language-text"><code>array([[[[ 30,  24,  18]],
         [[ 84,  69,  54]],
         [[138, 114,  90]]]])
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="matplotlib绘图" tabindex="-1"><a class="header-anchor" href="#matplotlib绘图" aria-hidden="true">#</a> Matplotlib绘图</h2><p><code>from matplotlib import pyplot as plt</code></p><h3 id="线性函数图像" tabindex="-1"><a class="header-anchor" href="#线性函数图像" aria-hidden="true">#</a> 线性函数图像</h3><div class="language-text line-numbers-mode" data-ext="text"><pre class="language-text"><code>import numpy as np
from matplotlib import pyplot as plt
x = np.arange(1,11)
y = 2 * x + 5
#绘制坐标标题
plt.title(&quot;Matplotlib demo&quot;)
#绘制x、y轴备注
plt.xlabel(&quot;x axis&quot;)
plt.ylabel(&quot;y axis&quot;)
plt.plot(x,y)
plt.show()
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>可以向 plot() 函数中添加格式化字符，来实现不同样式的显示或标记。 下表列举了常用的格式化字符：</p><table><thead><tr><th>字符</th><th>描述</th></tr></thead><tbody><tr><td>&#39;-&#39;</td><td>实线样式</td></tr><tr><td>&#39;--&#39;</td><td>短横线样式</td></tr><tr><td>&#39;-.&#39;</td><td>点划线样式</td></tr><tr><td>&#39;:&#39;</td><td>虚线样式</td></tr><tr><td>&#39;.&#39;</td><td>点标记</td></tr><tr><td>&#39;,&#39;</td><td>像素标记</td></tr><tr><td>&#39;o&#39;</td><td>圆标记</td></tr><tr><td>&#39;v&#39;</td><td>倒三角标记</td></tr><tr><td>&#39;^&#39;</td><td>正三角标记</td></tr><tr><td>&#39;&lt;&#39;</td><td>左三角标记</td></tr><tr><td>&#39;&gt;&#39;</td><td>右三角标记</td></tr><tr><td>&#39;1&#39;</td><td>下箭头标记</td></tr><tr><td>&#39;2&#39;</td><td>上箭头标记</td></tr><tr><td>&#39;3&#39;</td><td>左箭头标记</td></tr><tr><td>&#39;4&#39;</td><td>右箭头标记</td></tr><tr><td>&#39;s&#39;</td><td>正方形标记</td></tr><tr><td>&#39;p&#39;</td><td>五边形标记</td></tr><tr><td>&#39;*&#39;</td><td>星形标记</td></tr><tr><td>&#39;h&#39;</td><td>六边形标记 1</td></tr><tr><td>&#39;H&#39;</td><td>六边形标记 2</td></tr><tr><td>&#39;+&#39;</td><td>加号标记</td></tr><tr><td>&#39;x&#39;</td><td>X 标记</td></tr><tr><td>&#39;D&#39;</td><td>菱形标记</td></tr><tr><td>&#39;d&#39;</td><td>窄菱形标记</td></tr><tr><td>&#39;|&#39;</td><td>竖直线标记</td></tr><tr><td>&#39;_&#39;</td><td>水平线标记</td></tr></tbody></table><p>Matplotlib 还定义了一些颜色字符，如下所示：</p><table><thead><tr><th>字符</th><th>颜色</th></tr></thead><tbody><tr><td>&#39;b&#39;</td><td>蓝色</td></tr><tr><td>&#39;g&#39;</td><td>绿色</td></tr><tr><td>&#39;r&#39;</td><td>红色</td></tr><tr><td>&#39;c&#39;</td><td>青色</td></tr><tr><td>&#39;m&#39;</td><td>品红色</td></tr><tr><td>&#39;y&#39;</td><td>黄色</td></tr><tr><td>&#39;k&#39;</td><td>黑色</td></tr><tr><td>&#39;w&#39;</td><td>白色</td></tr></tbody></table><p>如果想要以圆点的样式，来代替图 1 中的线条样式，那么可以使用“ ob”作为 plot() 的格式化字符。</p><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
<span class="token keyword">from</span> matplotlib <span class="token keyword">import</span> pyplot <span class="token keyword">as</span> plt
x <span class="token operator">=</span> np<span class="token punctuation">.</span>arange<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">11</span><span class="token punctuation">)</span>
y <span class="token operator">=</span> <span class="token number">2</span> <span class="token operator">*</span> x <span class="token operator">+</span> <span class="token number">5</span>
plt<span class="token punctuation">.</span>title<span class="token punctuation">(</span><span class="token string">&quot;Matplotlib demo1&quot;</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>xlabel<span class="token punctuation">(</span><span class="token string">&quot;x axis&quot;</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>ylabel<span class="token punctuation">(</span><span class="token string">&quot;y axis&quot;</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>plot<span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">,</span><span class="token string">&quot;ob&quot;</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span> 
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="正弦波图" tabindex="-1"><a class="header-anchor" href="#正弦波图" aria-hidden="true">#</a> 正弦波图</h3><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
<span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt 
<span class="token comment"># 计算正弦曲线上的x和y坐标</span>
x <span class="token operator">=</span> np<span class="token punctuation">.</span>arange<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">3</span> <span class="token operator">*</span> np<span class="token punctuation">.</span>pi<span class="token punctuation">,</span> <span class="token number">0.1</span><span class="token punctuation">)</span>
y <span class="token operator">=</span> np<span class="token punctuation">.</span>sin<span class="token punctuation">(</span>x<span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>title<span class="token punctuation">(</span><span class="token string">&quot;sine wave image&quot;</span><span class="token punctuation">)</span>
<span class="token comment"># 使用matplotlib制图</span>
plt<span class="token punctuation">.</span>plot<span class="token punctuation">(</span>x<span class="token punctuation">,</span> y<span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span> 
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="subplot" tabindex="-1"><a class="header-anchor" href="#subplot" aria-hidden="true">#</a> subplot()</h3><p>允许您在同一画布中的不同位置绘制多个图像，可以理解为对画布按行、列分割</p><p><code>plt.subplot(nrows, ncols, index, **kwargs)</code></p><p>参数说明：该函数使用三个整数描述子图的位置信息，这三个整数是行数、列数和索引值（此处索引值从1开始），子图将分布在设定的索引位置上。从右上角增加到右下角。比如，plt.subplot(2, 3, 5) 表示子图位于 2 行 3 列 中的第 5 个位置上。</p><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
<span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt 
  
<span class="token comment">#计算正弦和余弦曲线上的点的 x 和 y 坐标 </span>
x <span class="token operator">=</span> np<span class="token punctuation">.</span>arange<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">3</span> <span class="token operator">*</span> np<span class="token punctuation">.</span>pi<span class="token punctuation">,</span> <span class="token number">0.1</span><span class="token punctuation">)</span>
y_sin <span class="token operator">=</span> np<span class="token punctuation">.</span>sin<span class="token punctuation">(</span>x<span class="token punctuation">)</span>
y_cos <span class="token operator">=</span> np<span class="token punctuation">.</span>cos<span class="token punctuation">(</span>x<span class="token punctuation">)</span> 
  
<span class="token comment">#绘制subplot 网格为2行1列</span>
<span class="token comment">#激活第一个 subplot</span>
plt<span class="token punctuation">.</span>subplot<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span>
<span class="token comment">#绘制第一个图像</span>
plt<span class="token punctuation">.</span>plot<span class="token punctuation">(</span>x<span class="token punctuation">,</span> y_sin<span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>title<span class="token punctuation">(</span><span class="token string">&#39;Sine&#39;</span><span class="token punctuation">)</span> 

<span class="token comment">#将第二个 subplot 激活，并绘制第二个图像</span>
plt<span class="token punctuation">.</span>subplot<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>plot<span class="token punctuation">(</span>x<span class="token punctuation">,</span> y_cos<span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>title<span class="token punctuation">(</span><span class="token string">&#39;Cosine&#39;</span><span class="token punctuation">)</span>
<span class="token comment">#展示图像</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="bar-柱状图" tabindex="-1"><a class="header-anchor" href="#bar-柱状图" aria-hidden="true">#</a> bar()柱状图</h3><div class="language-text line-numbers-mode" data-ext="text"><pre class="language-text"><code>from matplotlib import pyplot as plt
#第一组数据
x1 = [5,8,10]
y1 = [12,16,6] 
#第二组数据
x2 = [6,9,11]
y2 = [6,15,7]
plt.bar(x1, y1, align = &#39;center&#39;)
plt.bar(x2, y2, color = &#39;g&#39;, align = &#39;center&#39;)
plt.title(&#39;Bar graph&#39;)
#设置x轴与y轴刻度
plt.ylabel(&#39;Y axis&#39;)
plt.xlabel(&#39;X axis&#39;) 
plt.show()
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="numpy-histogram" tabindex="-1"><a class="header-anchor" href="#numpy-histogram" aria-hidden="true">#</a> numpy.histogram()</h2><p>直方图</p><h3 id="plt" tabindex="-1"><a class="header-anchor" href="#plt" aria-hidden="true">#</a> plt()</h3><p>直方图</p><h2 id="io操作" tabindex="-1"><a class="header-anchor" href="#io操作" aria-hidden="true">#</a> IO操作</h2><p>NumPy IO 操作是以文件的形式从磁盘中加载 ndarray 对象。在这个过程中，NumPy 可以两种文件类型处理 ndarray 对象，一类是二进制文件（以<code>.npy</code>结尾），另一类是普通文本文件。</p><table><thead><tr><th>文件类型</th><th>处理方法</th></tr></thead><tbody><tr><td>二进制文件</td><td>load() 和 save()</td></tr><tr><td>普通文本文件</td><td>loadtxt() 和 savetxt()</td></tr></tbody></table><h3 id="numpy-save" tabindex="-1"><a class="header-anchor" href="#numpy-save" aria-hidden="true">#</a> numpy.save()</h3><p>numpy.save() 方法将输入数组存储在<code>.npy</code>文件中。</p><p><code>numpy.save(file, arr, allow_pickle=True, fix_imports=True)</code></p><ul><li>file：保存后的文件名称，其文件类型为<code>.npy</code>；</li><li>arr：要保存的数组</li><li>allow_pickle：可选项，布尔值参数，允许使用 pickle 序列化保存数组对象。</li><li>fix_imports：可选项，为了便于在 Pyhton2 版本中读取 Python3 保存的数据。</li></ul><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
a <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
np<span class="token punctuation">.</span>save<span class="token punctuation">(</span><span class="token string">&#39;first&#39;</span><span class="token punctuation">,</span>a<span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>使用 load() 从 first.npy 文件中加载数据，如下所示：</p><div class="language-python line-numbers-mode" data-ext="py"><pre class="language-python"><code><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
b <span class="token operator">=</span> np<span class="token punctuation">.</span>load<span class="token punctuation">(</span><span class="token string">&#39;outfile.npy&#39;</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span> b<span class="token punctuation">)</span> 
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="savetxt" tabindex="-1"><a class="header-anchor" href="#savetxt" aria-hidden="true">#</a> savetxt()</h3><p>savetxt() 和 loadtxt() 分别表示以文本格式存储数据或加载数据。</p><p><code>np.savetxt(&#39;filename文件路径&#39;, self.task, fmt=&quot;%d&quot;, delimiter=&quot; &quot;)</code></p><ul><li>filename：表示保存文件的路径；</li><li>self.task： 要保存数组的变量名；</li><li>fmt=&quot;%d&quot;： 指定保存文件的格式，默认是十进制；</li><li>delimiter=&quot; &quot;表示分隔符，默认以空格的形式隔开。</li></ul><div class="language-text line-numbers-mode" data-ext="text"><pre class="language-text"><code>import numpy as np
a = np.array([1,2,3,4,5])
np.savetxt(&#39;second.txt&#39;,a)
#使用loadtxt重载数据
b = np.loadtxt(&#39;second.txt&#39;)
print(b) 
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div></div><!--[--><!--]--></div><footer class="page-meta"><div class="meta-item edit-link"><a class="external-link meta-item-label" href="https://gitee.com/Francis-xsc/francis-xsc/edit/main/program/Numpy.md" rel="noopener noreferrer" target="_blank" aria-label="Edit this page"><!--[--><!--]--> Edit this page <span><svg class="external-link-icon" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg><span class="external-link-icon-sr-only">open in new window</span></span><!--[--><!--]--></a></div><div class="meta-item last-updated"><span class="meta-item-label">最后更新时间: </span><!----></div><div class="meta-item contributors"><span class="meta-item-label">Contributors: </span><span class="meta-item-info"><!--[--><!--[--><span class="contributor" title="email: 920364365@qq.com">Francis-xsc</span><!----><!--]--><!--]--></span></div></footer><!----><!--[--><!--]--></main><!--]--></div><!----><!--]--></div>
    <script type="module" src="/assets/app-6c74e544.js" defer></script>
  </body>
</html>
